File: flashwrite.c

package info (click to toggle)
nwutil 1.4-3
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 132 kB
  • ctags: 86
  • sloc: ansic: 1,176; makefile: 63
file content (73 lines) | stat: -rw-r--r-- 1,143 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>

#include "flashlogo.h"

int flashsize(void)
{
	int fd = 0;
	int size = 0;
	
	fd = open("/dev/nwflash", O_RDONLY);
	
	if (fd <= 0)
		return FLASH_EACCES;
		
	if (lseek(fd, 4*1024*1024, SEEK_SET) > 0)
		size = 4*1024*1024;
	else if (lseek(fd, 1024*1024, SEEK_SET) > 0)
		size = 1024*1024;
	else size = FLASH_EBADSIZE; 
	
	close(fd);
	
	return size;
}		

int flashread(char *buffer, int offset, int size)
{
	int fd = 0;
	int ret;
	
	fd = open("/dev/nwflash", O_RDONLY);
	
	if (fd <= 0)
		return FLASH_EACCES;

	if (lseek(fd, offset, SEEK_SET) < 0)
		return FLASH_EBADOFF;
		
	ret = read(fd, buffer, size);
	
	close(fd);

	return ret;
}

int flashwrite(char *buffer, int offset, int size)
{
	int fd = 0;
	int ret;
	
	fd = open("/dev/nwflash", O_RDWR);
	
	if (fd <= 0)
		return FLASH_EACCES;

	if (lseek(fd, offset, SEEK_SET) < 0)
		return FLASH_EBADOFF;
		
	ioctl(fd, CMD_WRITE_ENABLE);
	ret = write(fd, buffer, size);
	ioctl(fd, CMD_WRITE_DISABLE);
		
	close(fd);

	return ret;
}