1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * $Id: main-e200r-installer.c 15599 2007-11-12 18:49:53Z amiconn $
10 * Copyright (C) 2011 by Frank Gevaerts
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
32 #include "crc32-mi4.h"
35 #include "backlight-target.h"
38 unsigned char zero
[1024*64];
39 unsigned char nonzero
[1024*64];
40 unsigned char scratch
[1024*64];
41 struct storage_info info
;
43 int format_partition(int start
, int size
);
50 return lba
/(SPT
* HPC
);
61 int write_mbr(int datastart
,int datasize
, int firmwarestart
, int firmwaresize
)
63 unsigned char mbr
[512];
65 mbr
[446]=0x80; /* flags */
66 mbr
[447]=h(datastart
); /* chs1[0] (h) */
67 mbr
[448]=s(datastart
); /* chs1[1] (s) */
68 mbr
[449]=c(datastart
); /* chs1[2] (c) */
69 mbr
[450]=0x0b; /* type */
70 mbr
[451]=h(datastart
+datasize
-1); /* chs2[0] (h) */
71 mbr
[452]=s(datastart
+datasize
-1); /* chs2[1] (s) */
72 mbr
[453]=c(datastart
+datasize
-1); /* chs2[2] (c) */
73 mbr
[454]=(datastart
&0x000000ff); /* lba[0] */
74 mbr
[455]=(datastart
&0x0000ff00) >>8; /* lba[1] */
75 mbr
[456]=(datastart
&0x00ff0000) >>16; /* lba[2] */
76 mbr
[457]=(datastart
&0xff000000) >>24; /* lba[3] */
77 mbr
[458]=(datasize
&0x000000ff); /* size[0] */
78 mbr
[459]=(datasize
&0x0000ff00) >>8; /* size[1] */
79 mbr
[460]=(datasize
&0x00ff0000) >>16; /* size[2] */
80 mbr
[461]=(datasize
&0xff000000) >>24; /* size[3] */
82 mbr
[462]=0; /* flags */
83 mbr
[463]=h(firmwarestart
); /* chs1[0] (h) */
84 mbr
[464]=s(firmwarestart
); /* chs1[1] (s) */
85 mbr
[465]=c(firmwarestart
); /* chs1[2] (c) */
86 mbr
[466]=0x84; /* type */
87 mbr
[467]=h(firmwarestart
+firmwaresize
-1); /* chs2[0] (h) */
88 mbr
[468]=s(firmwarestart
+firmwaresize
-1); /* chs2[1] (s) */
89 mbr
[469]=c(firmwarestart
+firmwaresize
-1); /* chs2[2] (c) */
90 mbr
[470]=(firmwarestart
&0x000000ffu
); /* lba[0] */
91 mbr
[471]=(firmwarestart
&0x0000ff00u
) >>8; /* lba[1] */
92 mbr
[472]=(firmwarestart
&0x00ff0000u
) >>16; /* lba[2] */
93 mbr
[473]=(firmwarestart
&0xff000000u
) >>24; /* lba[3] */
94 mbr
[474]=(firmwaresize
&0x000000ffu
); /* size[0] */
95 mbr
[475]=(firmwaresize
&0x0000ff00u
) >>8; /* size[1] */
96 mbr
[476]=(firmwaresize
&0x00ff0000u
) >>16; /* size[2] */
97 mbr
[477]=(firmwaresize
&0xff000000u
) >>24; /* size[3] */
102 int res
= storage_write_sectors(0,1,mbr
);
110 /* Hack. We "steal" line from common.c to reset the line number
111 * so we can overwrite the previous line for nicer progress info
114 int wipe(int size
, int verify
)
118 int sectors
= sizeof(nonzero
)/512;
119 for(i
=0;i
<size
;i
+=sectors
)
123 res
= storage_write_sectors(i
,sectors
,nonzero
);
126 printf("write error (1) on sector %d (of %d)!",i
,size
);
129 res
= storage_read_sectors(i
,sectors
,scratch
);
132 printf("read error (1) on sector %d (of %d)!",i
,size
);
135 res
= memcmp(nonzero
, scratch
, sizeof(nonzero
));
138 printf("compare error (1) on sector %d (of %d)!",i
,size
);
142 res
= storage_write_sectors(i
,sectors
,zero
);
145 printf("write error (2) on sector %d (of %d)!",i
,size
);
150 res
= storage_read_sectors(i
,sectors
,scratch
);
153 printf("read error (2) on sector %d (of %d)!",i
,size
);
156 res
= memcmp(zero
, scratch
, sizeof(nonzero
));
159 printf("compare error (2) on sector %d (of %d)!",i
,size
);
166 printf("%d of %d MB done",i
/2048, size
/2048);
167 /* Hack to overwrite the previous line */
179 chksum_crc32gentab ();
189 lcd_set_foreground(LCD_WHITE
);
190 lcd_set_background(LCD_BLACK
);
193 btn
= button_read_device();
196 lcd_setfont(FONT_SYSFIXED
);
198 printf("Sansa initialiser");
205 storage_get_info(0,&info
);
206 int size
= info
.num_sectors
;
207 memset(zero
,0,sizeof(zero
));
208 memset(nonzero
,0xff,sizeof(nonzero
));
209 printf("Zeroing flash");
215 printf("error wiping flash");
218 int firmwaresize
= 0xa000;
219 int firmwarestart
= size
- firmwaresize
;
221 int datasize
= firmwarestart
- datastart
;
223 res
= write_mbr(datastart
,datasize
,firmwarestart
,firmwaresize
);
226 printf("error writing mbr");
228 res
= format_partition(datastart
, datasize
);
231 printf("error formatting");
234 printf("Wipe done.");
236 printf("Release Hold and");
237 printf("press any key to");
240 printf("Remember to use");
241 printf("manufacturing");
242 printf("mode to recover");
245 while(button_read_device() == BUTTON_NONE
);