1 #define PROGRAM_NAME "doc_loadbios"
10 #include <sys/ioctl.h>
11 #include <sys/mount.h>
13 #include <mtd/mtd-user.h>
15 unsigned char databuf[512];
17 int main(int argc,char **argv)
23 unsigned long retlen, ofs, iplsize, ipltailsize;
24 unsigned char *iplbuf;
28 fprintf(stderr,"You must specify a device,"
29 " the source firmware file and the offset\n");
33 // Open and size the device
34 if ((ofd = open(argv[1],O_RDWR)) < 0) {
35 perror("Open flash device");
39 if ((ifd = open(argv[2], O_RDONLY)) < 0) {
40 perror("Open firmware file\n");
45 if (fstat(ifd, &statbuf) != 0) {
46 perror("Stat firmware file");
51 if (statbuf.st_size > 65536) {
52 printf("Firmware too large (%ld bytes)\n",statbuf.st_size);
57 if (ioctl(ofd,MEMGETINFO,&meminfo) != 0) {
58 perror("ioctl(MEMGETINFO)");
62 iplsize = (ipltailsize = 0);
64 /* DoC Millennium has IPL in the first 1K of flash memory */
65 /* You may want to specify the offset 1024 to store
66 the firmware next to IPL. */
67 iplsize = strtoul(argv[3], NULL, 0);
68 ipltailsize = iplsize % meminfo.erasesize;
71 if (lseek(ofd, iplsize - ipltailsize, SEEK_SET) < 0) {
77 iplbuf = malloc(ipltailsize);
79 fprintf(stderr, "Not enough memory for IPL tail buffer of"
80 " %lu bytes\n", (unsigned long) ipltailsize);
83 printf("Reading IPL%s area of length %lu at offset %lu\n",
84 (iplsize - ipltailsize) ? " tail" : "",
85 (long unsigned) ipltailsize,
86 (long unsigned) (iplsize - ipltailsize));
87 if (read(ofd, iplbuf, ipltailsize) != ipltailsize) {
93 erase.length = meminfo.erasesize;
95 for (ofs = iplsize - ipltailsize ;
96 ofs < iplsize + statbuf.st_size ;
97 ofs += meminfo.erasesize) {
99 printf("Performing Flash Erase of length %lu at offset %lu\n",
100 (long unsigned) erase.length, (long unsigned) erase.start);
102 if (ioctl(ofd,MEMERASE,&erase) != 0) {
103 perror("ioctl(MEMERASE)");
108 if (lseek(ofd, iplsize - ipltailsize, SEEK_SET) < 0) {
114 printf("Writing IPL%s area of length %lu at offset %lu\n",
115 (iplsize - ipltailsize) ? " tail" : "",
116 (long unsigned) ipltailsize,
117 (long unsigned) (iplsize - ipltailsize));
118 if (write(ofd, iplbuf, ipltailsize) != ipltailsize) {
124 printf("Writing the firmware of length %lu at %lu... ",
125 (unsigned long) statbuf.st_size,
126 (unsigned long) iplsize);
128 retlen = read(ifd, databuf, 512);
130 memset(databuf+retlen, 0xff, 512-retlen);
131 if (write(ofd, databuf, 512) != 512) {
135 } while (retlen == 512);