skin engine: Relax the AA load width/height checks
[maemo-rb.git] / bootloader / main-ppsansawipe.c
blob3221a096829a909dac0440e4280398d10e6768f4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
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 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "common.h"
23 #include "cpu.h"
24 #include "file.h"
25 #include "system.h"
26 #include "kernel.h"
27 #include "lcd.h"
28 #include "font.h"
29 #include "storage.h"
30 #include "button.h"
31 #include "disk.h"
32 #include "crc32-mi4.h"
33 #include <string.h>
34 #include "i2c.h"
35 #include "backlight-target.h"
36 #include "power.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);
45 #define SPT 63
46 #define HPC 255
48 int c(int lba)
50 return lba/(SPT * HPC);
52 int h(int lba)
54 return (lba/SPT)%HPC;
56 int s(int lba)
58 return (lba%SPT) + 1;
61 int write_mbr(int datastart,int datasize, int firmwarestart, int firmwaresize)
63 unsigned char mbr[512];
64 memset(mbr,0,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] */
99 mbr[510]=0x55;
100 mbr[511]=0xaa;
102 int res = storage_write_sectors(0,1,mbr);
103 if(res != 0)
105 return -1;
107 return 0;
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
113 extern int line;
114 int wipe(int size, int verify)
116 int i;
117 int res;
118 int sectors = sizeof(nonzero)/512;
119 for(i=0;i<size;i+=sectors)
121 if(verify)
123 res = storage_write_sectors(i,sectors,nonzero);
124 if(res != 0)
126 printf("write error (1) on sector %d (of %d)!",i,size);
127 return -1;
129 res = storage_read_sectors(i,sectors,scratch);
130 if(res != 0)
132 printf("read error (1) on sector %d (of %d)!",i,size);
133 return -1;
135 res = memcmp(nonzero, scratch, sizeof(nonzero));
136 if(res != 0)
138 printf("compare error (1) on sector %d (of %d)!",i,size);
139 return -1;
142 res = storage_write_sectors(i,sectors,zero);
143 if(res != 0)
145 printf("write error (2) on sector %d (of %d)!",i,size);
146 return -1;
148 if(verify)
150 res = storage_read_sectors(i,sectors,scratch);
151 if(res != 0)
153 printf("read error (2) on sector %d (of %d)!",i,size);
154 return -1;
156 res = memcmp(zero, scratch, sizeof(nonzero));
157 if(res != 0)
159 printf("compare error (2) on sector %d (of %d)!",i,size);
160 return -1;
164 if(i%2048 == 0)
166 printf("%d of %d MB done",i/2048, size/2048);
167 /* Hack to overwrite the previous line */
168 line--;
171 return 0;
174 void* main(void)
176 int i;
177 int btn;
179 chksum_crc32gentab ();
181 system_init();
182 kernel_init();
183 lcd_init();
184 font_init();
185 button_init();
186 i2c_init();
187 _backlight_on();
189 lcd_set_foreground(LCD_WHITE);
190 lcd_set_background(LCD_BLACK);
191 lcd_clear_display();
193 btn = button_read_device();
194 verbose = true;
196 lcd_setfont(FONT_SYSFIXED);
198 printf("Sansa initialiser");
199 printf("");
202 i=storage_init();
203 disk_init(IF_MD(0));
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");
210 int res;
212 res = wipe(size, 0);
213 if(res != 0)
215 printf("error wiping flash");
218 int firmwaresize = 0xa000;
219 int firmwarestart = size - firmwaresize;
220 int datastart = 600;
221 int datasize = firmwarestart - datastart;
223 res = write_mbr(datastart,datasize,firmwarestart,firmwaresize);
224 if(res != 0)
226 printf("error writing mbr");
228 res = format_partition(datastart, datasize);
229 if(res != 0)
231 printf("error formatting");
234 printf("Wipe done.");
235 if (button_hold())
236 printf("Release Hold and");
237 printf("press any key to");
238 printf("shutdown.");
240 printf("Remember to use");
241 printf("manufacturing");
242 printf("mode to recover");
243 printf("further");
245 while(button_read_device() == BUTTON_NONE);
247 power_off();
249 return NULL;