blob: 0060306d813251d95c86c2df4a0c00778763c652 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08005 * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Code Aurora nor
15 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
36#include <dev/udc.h>
37#include <string.h>
38#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044
Brian Swetland9c4c0752009-01-25 16:23:50 -080045#include "bootimg.h"
46#include "fastboot.h"
47
Chandan Uddarajuda919832009-11-17 01:06:11 -080048#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070049
David Ng183a7422009-12-07 14:55:21 -080050#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080051#define RECOVERY_MODE 0x77665502
52#define FASTBOOT_MODE 0x77665500
53
David Ng183a7422009-12-07 14:55:21 -080054static const char *emmc_cmdline = " androidboot.emmc=true";
55
Brian Swetland9c4c0752009-01-25 16:23:50 -080056static struct udc_device surf_udc_device = {
57 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080058 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080059 .version_id = 0x0100,
60 .manufacturer = "Google",
61 .product = "Android",
62};
63
Dima Zavin42168f22009-01-30 11:52:22 -080064struct atag_ptbl_entry
65{
66 char name[16];
67 unsigned offset;
68 unsigned size;
69 unsigned flags;
70};
71
Brian Swetland9c4c0752009-01-25 16:23:50 -080072void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080073unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080074unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080075unsigned check_reboot_mode(void);
David Ng183a7422009-12-07 14:55:21 -080076int target_is_emmc_boot(void);
Brian Swetland9c4c0752009-01-25 16:23:50 -080077
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080078static int boot_into_recovery = 0;
79
Dima Zavin42168f22009-01-30 11:52:22 -080080static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
81{
82 struct atag_ptbl_entry atag_ptn;
83
84 memcpy(atag_ptn.name, ptn->name, 16);
85 atag_ptn.name[15] = '\0';
86 atag_ptn.offset = ptn->start;
87 atag_ptn.size = ptn->length;
88 atag_ptn.flags = ptn->flags;
89 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
90 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
91}
Brian Swetland9c4c0752009-01-25 16:23:50 -080092
93void boot_linux(void *kernel, unsigned *tags,
94 const char *cmdline, unsigned machtype,
95 void *ramdisk, unsigned ramdisk_size)
96{
97 unsigned *ptr = tags;
98 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -080099 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800100 int cmdline_len = 0;
101 int have_cmdline = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800102
103 /* CORE */
104 *ptr++ = 2;
105 *ptr++ = 0x54410001;
106
107 if (ramdisk_size) {
108 *ptr++ = 4;
109 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800110 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800111 *ptr++ = ramdisk_size;
112 }
113
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800114 ptr = target_atag_mem(ptr);
115
David Ng183a7422009-12-07 14:55:21 -0800116 if (!target_is_emmc_boot()) {
117 /* Skip NAND partition ATAGS for eMMC boot */
118 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
119 int i;
120 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
121 sizeof(unsigned)));
122 *ptr++ = 0x4d534d70;
123 for (i = 0; i < ptable->count; ++i)
124 ptentry_to_tag(&ptr, ptable_get(ptable, i));
125 }
Dima Zavin42168f22009-01-30 11:52:22 -0800126 }
127
Brian Swetland9c4c0752009-01-25 16:23:50 -0800128 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800129 cmdline_len = strlen(cmdline);
130 have_cmdline = 1;
131 }
132 if (target_is_emmc_boot()) {
133 cmdline_len += strlen(emmc_cmdline);
134 }
135 if (cmdline_len > 0) {
136 const char *src;
137 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800138 unsigned n;
139 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800140 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800141 *ptr++ = (n / 4) + 2;
142 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800143 dst = (char *)ptr;
144 if (have_cmdline) {
145 src = cmdline;
146 while ((*dst++ = *src++));
147 }
148 if (target_is_emmc_boot()) {
149 src = emmc_cmdline;
150 if (have_cmdline) --dst;
151 while ((*dst++ = *src++));
152 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800153 ptr += (n / 4);
154 }
155
156 /* END */
157 *ptr++ = 0;
158 *ptr++ = 0;
159
160 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
161 kernel, ramdisk, ramdisk_size);
162 if (cmdline)
163 dprintf(INFO, "cmdline: %s\n", cmdline);
164
165 enter_critical_section();
166 platform_uninit_timer();
167 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800168 arch_disable_mmu();
169
170 entry(0, machtype, tags);
171
172}
173
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800174unsigned page_size = 0;
175unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800176
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800177#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800178
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800179static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800180
181int boot_linux_from_flash(void)
182{
183 struct boot_img_hdr *hdr = (void*) buf;
184 unsigned n;
185 struct ptentry *ptn;
186 struct ptable *ptable;
187 unsigned offset = 0;
188 const char *cmdline;
189
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800190
David Ng183a7422009-12-07 14:55:21 -0800191 if (target_is_emmc_boot()) {
192 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
193 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
194 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
195 return -1;
196 }
197 goto continue_boot;
198 }
199
Dima Zavin214cc642009-01-26 11:16:21 -0800200 ptable = flash_get_ptable();
201 if (ptable == NULL) {
202 dprintf(CRITICAL, "ERROR: Partition table not found\n");
203 return -1;
204 }
205
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800206 if(!boot_into_recovery)
207 {
208 ptn = ptable_find(ptable, "boot");
209 if (ptn == NULL) {
210 dprintf(CRITICAL, "ERROR: No boot partition found\n");
211 return -1;
212 }
213 }
214 else
215 {
216 ptn = ptable_find(ptable, "recovery");
217 if (ptn == NULL) {
218 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
219 return -1;
220 }
Dima Zavin214cc642009-01-26 11:16:21 -0800221 }
222
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800223 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800224 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
225 return -1;
226 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800227 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800228
229 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
230 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
231 return -1;
232 }
233
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800234 if (hdr->page_size != page_size) {
235 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
236 return -1;
237 }
238
239 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800240 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
241 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
242 return -1;
243 }
244 offset += n;
245
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800246 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800247 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
248 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
249 return -1;
250 }
251 offset += n;
252
David Ng183a7422009-12-07 14:55:21 -0800253continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800254 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
255 hdr->kernel_size);
256 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
257 hdr->ramdisk_size);
258
259 if(hdr->cmdline[0]) {
260 cmdline = (char*) hdr->cmdline;
261 } else {
262 cmdline = DEFAULT_CMDLINE;
263 }
264 dprintf(INFO, "cmdline = '%s'\n", cmdline);
265
266 /* TODO: create/pass atags to kernel */
267
268 dprintf(INFO, "\nBooting Linux\n");
269 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800270 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800271 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
272
273 return 0;
274}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800275
276void cmd_boot(const char *arg, void *data, unsigned sz)
277{
278 unsigned kernel_actual;
279 unsigned ramdisk_actual;
280 static struct boot_img_hdr hdr;
281 char *ptr = ((char*) data);
282
283 if (sz < sizeof(hdr)) {
284 fastboot_fail("invalid bootimage header");
285 return;
286 }
287
288 memcpy(&hdr, data, sizeof(hdr));
289
290 /* ensure commandline is terminated */
291 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
292
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800293 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
294 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800295
296 if (2048 + kernel_actual + ramdisk_actual < sz) {
297 fastboot_fail("incomplete bootimage");
298 return;
299 }
300
301 memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
302 memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);
303
304 fastboot_okay("");
305 udc_stop();
306
Brian Swetland9c4c0752009-01-25 16:23:50 -0800307 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800308 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800309 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
310}
311
Dima Zavin214cc642009-01-26 11:16:21 -0800312void cmd_erase(const char *arg, void *data, unsigned sz)
313{
314 struct ptentry *ptn;
315 struct ptable *ptable;
316
317 ptable = flash_get_ptable();
318 if (ptable == NULL) {
319 fastboot_fail("partition table doesn't exist");
320 return;
321 }
322
323 ptn = ptable_find(ptable, arg);
324 if (ptn == NULL) {
325 fastboot_fail("unknown partition name");
326 return;
327 }
328
329 if (flash_erase(ptn)) {
330 fastboot_fail("failed to erase partition");
331 return;
332 }
333 fastboot_okay("");
334}
335
336void cmd_flash(const char *arg, void *data, unsigned sz)
337{
338 struct ptentry *ptn;
339 struct ptable *ptable;
340 unsigned extra = 0;
341
342 ptable = flash_get_ptable();
343 if (ptable == NULL) {
344 fastboot_fail("partition table doesn't exist");
345 return;
346 }
347
348 ptn = ptable_find(ptable, arg);
349 if (ptn == NULL) {
350 fastboot_fail("unknown partition name");
351 return;
352 }
353
354 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
355 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
356 fastboot_fail("image is not a boot image");
357 return;
358 }
359 }
360
361 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800362 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800363 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800364 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800365
366 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
367 if (flash_write(ptn, extra, data, sz)) {
368 fastboot_fail("flash write failure");
369 return;
370 }
371 dprintf(INFO, "partition '%s' updated\n", ptn->name);
372 fastboot_okay("");
373}
374
375void cmd_continue(const char *arg, void *data, unsigned sz)
376{
377 fastboot_okay("");
378 udc_stop();
379
380 boot_linux_from_flash();
381}
382
Brian Swetland9c4c0752009-01-25 16:23:50 -0800383void aboot_init(const struct app_descriptor *app)
384{
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800385 unsigned reboot_mode = 0;
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800386 page_size = flash_page_size();
387 page_mask = page_size - 1;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800388 if (keys_get_state(KEY_HOME) != 0)
389 boot_into_recovery = 1;
Dima Zavinb4283602009-01-26 16:36:57 -0800390 if (keys_get_state(KEY_BACK) != 0)
391 goto fastboot;
392
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800393 reboot_mode = check_reboot_mode();
394 if (reboot_mode == RECOVERY_MODE){
395 boot_into_recovery = 1;
396 }else if(reboot_mode == FASTBOOT_MODE){
397 goto fastboot;
398 }
399
Dima Zavinb4283602009-01-26 16:36:57 -0800400 boot_linux_from_flash();
401 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
402 "to fastboot mode.\n");
403
404fastboot:
Chandan Uddaraju24120502009-12-12 19:17:04 -0800405 display_init();
406 dprintf(INFO, "Diplay initialized\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -0800407 udc_init(&surf_udc_device);
408
409 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800410 fastboot_register("erase:", cmd_erase);
411 fastboot_register("flash:", cmd_flash);
412 fastboot_register("continue", cmd_continue);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800413 fastboot_publish("product", "swordfish");
414 fastboot_publish("kernel", "lk");
415
Brian Swetland2defe162009-08-18 14:35:59 -0700416 fastboot_init((void*) SCRATCH_ADDR, 100 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800417 udc_start();
418}
419
420APP_START(aboot)
421 .init = aboot_init,
422APP_END
423