blob: 6abe321059c0c1e439f3d7603604b4342f75df12 [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);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080077void reboot_device(unsigned);
Brian Swetland9c4c0752009-01-25 16:23:50 -080078
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080079static int boot_into_recovery = 0;
80
Dima Zavin42168f22009-01-30 11:52:22 -080081static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
82{
83 struct atag_ptbl_entry atag_ptn;
84
85 memcpy(atag_ptn.name, ptn->name, 16);
86 atag_ptn.name[15] = '\0';
87 atag_ptn.offset = ptn->start;
88 atag_ptn.size = ptn->length;
89 atag_ptn.flags = ptn->flags;
90 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
91 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
92}
Brian Swetland9c4c0752009-01-25 16:23:50 -080093
94void boot_linux(void *kernel, unsigned *tags,
95 const char *cmdline, unsigned machtype,
96 void *ramdisk, unsigned ramdisk_size)
97{
98 unsigned *ptr = tags;
99 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800100 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800101 int cmdline_len = 0;
102 int have_cmdline = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800103
104 /* CORE */
105 *ptr++ = 2;
106 *ptr++ = 0x54410001;
107
108 if (ramdisk_size) {
109 *ptr++ = 4;
110 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800111 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800112 *ptr++ = ramdisk_size;
113 }
114
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800115 ptr = target_atag_mem(ptr);
116
David Ng183a7422009-12-07 14:55:21 -0800117 if (!target_is_emmc_boot()) {
118 /* Skip NAND partition ATAGS for eMMC boot */
119 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
120 int i;
121 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
122 sizeof(unsigned)));
123 *ptr++ = 0x4d534d70;
124 for (i = 0; i < ptable->count; ++i)
125 ptentry_to_tag(&ptr, ptable_get(ptable, i));
126 }
Dima Zavin42168f22009-01-30 11:52:22 -0800127 }
128
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800130 cmdline_len = strlen(cmdline);
131 have_cmdline = 1;
132 }
133 if (target_is_emmc_boot()) {
134 cmdline_len += strlen(emmc_cmdline);
135 }
136 if (cmdline_len > 0) {
137 const char *src;
138 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800139 unsigned n;
140 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800141 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800142 *ptr++ = (n / 4) + 2;
143 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800144 dst = (char *)ptr;
145 if (have_cmdline) {
146 src = cmdline;
147 while ((*dst++ = *src++));
148 }
149 if (target_is_emmc_boot()) {
150 src = emmc_cmdline;
151 if (have_cmdline) --dst;
152 while ((*dst++ = *src++));
153 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800154 ptr += (n / 4);
155 }
156
157 /* END */
158 *ptr++ = 0;
159 *ptr++ = 0;
160
161 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
162 kernel, ramdisk, ramdisk_size);
163 if (cmdline)
164 dprintf(INFO, "cmdline: %s\n", cmdline);
165
166 enter_critical_section();
167 platform_uninit_timer();
168 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800169 arch_disable_mmu();
170
171 entry(0, machtype, tags);
172
173}
174
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800175unsigned page_size = 0;
176unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800177
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800178#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800179
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800180static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800181
182int boot_linux_from_flash(void)
183{
184 struct boot_img_hdr *hdr = (void*) buf;
185 unsigned n;
186 struct ptentry *ptn;
187 struct ptable *ptable;
188 unsigned offset = 0;
189 const char *cmdline;
190
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800191
David Ng183a7422009-12-07 14:55:21 -0800192 if (target_is_emmc_boot()) {
193 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
194 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
195 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
196 return -1;
197 }
198 goto continue_boot;
199 }
200
Dima Zavin214cc642009-01-26 11:16:21 -0800201 ptable = flash_get_ptable();
202 if (ptable == NULL) {
203 dprintf(CRITICAL, "ERROR: Partition table not found\n");
204 return -1;
205 }
206
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800207 if(!boot_into_recovery)
208 {
209 ptn = ptable_find(ptable, "boot");
210 if (ptn == NULL) {
211 dprintf(CRITICAL, "ERROR: No boot partition found\n");
212 return -1;
213 }
214 }
215 else
216 {
217 ptn = ptable_find(ptable, "recovery");
218 if (ptn == NULL) {
219 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
220 return -1;
221 }
Dima Zavin214cc642009-01-26 11:16:21 -0800222 }
223
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800224 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800225 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
226 return -1;
227 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800228 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800229
230 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
231 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
232 return -1;
233 }
234
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800235 if (hdr->page_size != page_size) {
236 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
237 return -1;
238 }
239
240 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800241 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
242 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
243 return -1;
244 }
245 offset += n;
246
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800247 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800248 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
249 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
250 return -1;
251 }
252 offset += n;
253
David Ng183a7422009-12-07 14:55:21 -0800254continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800255 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
256 hdr->kernel_size);
257 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
258 hdr->ramdisk_size);
259
260 if(hdr->cmdline[0]) {
261 cmdline = (char*) hdr->cmdline;
262 } else {
263 cmdline = DEFAULT_CMDLINE;
264 }
265 dprintf(INFO, "cmdline = '%s'\n", cmdline);
266
267 /* TODO: create/pass atags to kernel */
268
269 dprintf(INFO, "\nBooting Linux\n");
270 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800271 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800272 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
273
274 return 0;
275}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800276
277void cmd_boot(const char *arg, void *data, unsigned sz)
278{
279 unsigned kernel_actual;
280 unsigned ramdisk_actual;
281 static struct boot_img_hdr hdr;
282 char *ptr = ((char*) data);
283
284 if (sz < sizeof(hdr)) {
285 fastboot_fail("invalid bootimage header");
286 return;
287 }
288
289 memcpy(&hdr, data, sizeof(hdr));
290
291 /* ensure commandline is terminated */
292 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
293
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800294 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
295 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800296
297 if (2048 + kernel_actual + ramdisk_actual < sz) {
298 fastboot_fail("incomplete bootimage");
299 return;
300 }
301
302 memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
303 memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);
304
305 fastboot_okay("");
306 udc_stop();
307
Brian Swetland9c4c0752009-01-25 16:23:50 -0800308 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800309 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800310 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
311}
312
Dima Zavin214cc642009-01-26 11:16:21 -0800313void cmd_erase(const char *arg, void *data, unsigned sz)
314{
315 struct ptentry *ptn;
316 struct ptable *ptable;
317
318 ptable = flash_get_ptable();
319 if (ptable == NULL) {
320 fastboot_fail("partition table doesn't exist");
321 return;
322 }
323
324 ptn = ptable_find(ptable, arg);
325 if (ptn == NULL) {
326 fastboot_fail("unknown partition name");
327 return;
328 }
329
330 if (flash_erase(ptn)) {
331 fastboot_fail("failed to erase partition");
332 return;
333 }
334 fastboot_okay("");
335}
336
337void cmd_flash(const char *arg, void *data, unsigned sz)
338{
339 struct ptentry *ptn;
340 struct ptable *ptable;
341 unsigned extra = 0;
342
343 ptable = flash_get_ptable();
344 if (ptable == NULL) {
345 fastboot_fail("partition table doesn't exist");
346 return;
347 }
348
349 ptn = ptable_find(ptable, arg);
350 if (ptn == NULL) {
351 fastboot_fail("unknown partition name");
352 return;
353 }
354
355 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
356 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
357 fastboot_fail("image is not a boot image");
358 return;
359 }
360 }
361
362 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800363 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800364 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800365 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800366
367 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
368 if (flash_write(ptn, extra, data, sz)) {
369 fastboot_fail("flash write failure");
370 return;
371 }
372 dprintf(INFO, "partition '%s' updated\n", ptn->name);
373 fastboot_okay("");
374}
375
376void cmd_continue(const char *arg, void *data, unsigned sz)
377{
378 fastboot_okay("");
379 udc_stop();
380
381 boot_linux_from_flash();
382}
383
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800384void cmd_reboot(const char *arg, void *data, unsigned sz)
385{
386 dprintf(INFO, "rebooting the device\n");
387 fastboot_okay("");
388 reboot_device(0);
389}
390
391void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
392{
393 dprintf(INFO, "rebooting the device\n");
394 fastboot_okay("");
395 reboot_device(FASTBOOT_MODE);
396}
397
Brian Swetland9c4c0752009-01-25 16:23:50 -0800398void aboot_init(const struct app_descriptor *app)
399{
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800400 unsigned reboot_mode = 0;
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800401 page_size = flash_page_size();
402 page_mask = page_size - 1;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800403 if (keys_get_state(KEY_HOME) != 0)
404 boot_into_recovery = 1;
Dima Zavinb4283602009-01-26 16:36:57 -0800405 if (keys_get_state(KEY_BACK) != 0)
406 goto fastboot;
Chandan Uddaraju26a56902010-01-13 18:26:56 -0800407 if (keys_get_state(KEY_CLEAR) != 0)
408 goto fastboot;
Dima Zavinb4283602009-01-26 16:36:57 -0800409
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800410 reboot_mode = check_reboot_mode();
411 if (reboot_mode == RECOVERY_MODE){
412 boot_into_recovery = 1;
413 }else if(reboot_mode == FASTBOOT_MODE){
414 goto fastboot;
415 }
416
Dima Zavinb4283602009-01-26 16:36:57 -0800417 boot_linux_from_flash();
418 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
419 "to fastboot mode.\n");
420
421fastboot:
Chandan Uddaraju24120502009-12-12 19:17:04 -0800422 display_init();
423 dprintf(INFO, "Diplay initialized\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -0800424 udc_init(&surf_udc_device);
425
426 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800427 fastboot_register("erase:", cmd_erase);
428 fastboot_register("flash:", cmd_flash);
429 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800430 fastboot_register("reboot", cmd_reboot);
431 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800432 fastboot_publish("product", "swordfish");
433 fastboot_publish("kernel", "lk");
434
Brian Swetland2defe162009-08-18 14:35:59 -0700435 fastboot_init((void*) SCRATCH_ADDR, 100 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800436 udc_start();
437}
438
439APP_START(aboot)
440 .init = aboot_init,
441APP_END
442