blob: 4fc76d8d2d432e4df51fa93c4cb6ee11bb26c05a [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
51static const char *emmc_cmdline = " androidboot.emmc=true";
52
Brian Swetland9c4c0752009-01-25 16:23:50 -080053static struct udc_device surf_udc_device = {
54 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080055 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080056 .version_id = 0x0100,
57 .manufacturer = "Google",
58 .product = "Android",
59};
60
Dima Zavin42168f22009-01-30 11:52:22 -080061struct atag_ptbl_entry
62{
63 char name[16];
64 unsigned offset;
65 unsigned size;
66 unsigned flags;
67};
68
Brian Swetland9c4c0752009-01-25 16:23:50 -080069void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080070unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080071unsigned board_machtype(void);
David Ng183a7422009-12-07 14:55:21 -080072int target_is_emmc_boot(void);
Brian Swetland9c4c0752009-01-25 16:23:50 -080073
Dima Zavin42168f22009-01-30 11:52:22 -080074static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
75{
76 struct atag_ptbl_entry atag_ptn;
77
78 memcpy(atag_ptn.name, ptn->name, 16);
79 atag_ptn.name[15] = '\0';
80 atag_ptn.offset = ptn->start;
81 atag_ptn.size = ptn->length;
82 atag_ptn.flags = ptn->flags;
83 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
84 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
85}
Brian Swetland9c4c0752009-01-25 16:23:50 -080086
87void boot_linux(void *kernel, unsigned *tags,
88 const char *cmdline, unsigned machtype,
89 void *ramdisk, unsigned ramdisk_size)
90{
91 unsigned *ptr = tags;
92 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -080093 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -080094 int cmdline_len = 0;
95 int have_cmdline = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -080096
97 /* CORE */
98 *ptr++ = 2;
99 *ptr++ = 0x54410001;
100
101 if (ramdisk_size) {
102 *ptr++ = 4;
103 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800104 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800105 *ptr++ = ramdisk_size;
106 }
107
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800108 ptr = target_atag_mem(ptr);
109
David Ng183a7422009-12-07 14:55:21 -0800110 if (!target_is_emmc_boot()) {
111 /* Skip NAND partition ATAGS for eMMC boot */
112 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
113 int i;
114 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
115 sizeof(unsigned)));
116 *ptr++ = 0x4d534d70;
117 for (i = 0; i < ptable->count; ++i)
118 ptentry_to_tag(&ptr, ptable_get(ptable, i));
119 }
Dima Zavin42168f22009-01-30 11:52:22 -0800120 }
121
Brian Swetland9c4c0752009-01-25 16:23:50 -0800122 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800123 cmdline_len = strlen(cmdline);
124 have_cmdline = 1;
125 }
126 if (target_is_emmc_boot()) {
127 cmdline_len += strlen(emmc_cmdline);
128 }
129 if (cmdline_len > 0) {
130 const char *src;
131 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800132 unsigned n;
133 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800134 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800135 *ptr++ = (n / 4) + 2;
136 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800137 dst = (char *)ptr;
138 if (have_cmdline) {
139 src = cmdline;
140 while ((*dst++ = *src++));
141 }
142 if (target_is_emmc_boot()) {
143 src = emmc_cmdline;
144 if (have_cmdline) --dst;
145 while ((*dst++ = *src++));
146 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800147 ptr += (n / 4);
148 }
149
150 /* END */
151 *ptr++ = 0;
152 *ptr++ = 0;
153
154 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
155 kernel, ramdisk, ramdisk_size);
156 if (cmdline)
157 dprintf(INFO, "cmdline: %s\n", cmdline);
158
159 enter_critical_section();
160 platform_uninit_timer();
161 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800162 arch_disable_mmu();
163
164 entry(0, machtype, tags);
165
166}
167
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800168unsigned page_size = 0;
169unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800170
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800171#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800172
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800173static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800174
175int boot_linux_from_flash(void)
176{
177 struct boot_img_hdr *hdr = (void*) buf;
178 unsigned n;
179 struct ptentry *ptn;
180 struct ptable *ptable;
181 unsigned offset = 0;
182 const char *cmdline;
183
David Ng183a7422009-12-07 14:55:21 -0800184 if (target_is_emmc_boot()) {
185 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
186 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
187 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
188 return -1;
189 }
190 goto continue_boot;
191 }
192
Dima Zavin214cc642009-01-26 11:16:21 -0800193 ptable = flash_get_ptable();
194 if (ptable == NULL) {
195 dprintf(CRITICAL, "ERROR: Partition table not found\n");
196 return -1;
197 }
198
199 ptn = ptable_find(ptable, "boot");
200 if (ptn == NULL) {
201 dprintf(CRITICAL, "ERROR: No boot partition found\n");
202 return -1;
203 }
204
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800205 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800206 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
207 return -1;
208 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800209 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800210
211 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
212 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
213 return -1;
214 }
215
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800216 if (hdr->page_size != page_size) {
217 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
218 return -1;
219 }
220
221 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800222 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
223 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
224 return -1;
225 }
226 offset += n;
227
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800228 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800229 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
230 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
231 return -1;
232 }
233 offset += n;
234
David Ng183a7422009-12-07 14:55:21 -0800235continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800236 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
237 hdr->kernel_size);
238 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
239 hdr->ramdisk_size);
240
241 if(hdr->cmdline[0]) {
242 cmdline = (char*) hdr->cmdline;
243 } else {
244 cmdline = DEFAULT_CMDLINE;
245 }
246 dprintf(INFO, "cmdline = '%s'\n", cmdline);
247
248 /* TODO: create/pass atags to kernel */
249
250 dprintf(INFO, "\nBooting Linux\n");
251 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800252 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800253 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
254
255 return 0;
256}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800257
258void cmd_boot(const char *arg, void *data, unsigned sz)
259{
260 unsigned kernel_actual;
261 unsigned ramdisk_actual;
262 static struct boot_img_hdr hdr;
263 char *ptr = ((char*) data);
264
265 if (sz < sizeof(hdr)) {
266 fastboot_fail("invalid bootimage header");
267 return;
268 }
269
270 memcpy(&hdr, data, sizeof(hdr));
271
272 /* ensure commandline is terminated */
273 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
274
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800275 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
276 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800277
278 if (2048 + kernel_actual + ramdisk_actual < sz) {
279 fastboot_fail("incomplete bootimage");
280 return;
281 }
282
283 memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
284 memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);
285
286 fastboot_okay("");
287 udc_stop();
288
Brian Swetland9c4c0752009-01-25 16:23:50 -0800289 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800290 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800291 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
292}
293
Dima Zavin214cc642009-01-26 11:16:21 -0800294void cmd_erase(const char *arg, void *data, unsigned sz)
295{
296 struct ptentry *ptn;
297 struct ptable *ptable;
298
299 ptable = flash_get_ptable();
300 if (ptable == NULL) {
301 fastboot_fail("partition table doesn't exist");
302 return;
303 }
304
305 ptn = ptable_find(ptable, arg);
306 if (ptn == NULL) {
307 fastboot_fail("unknown partition name");
308 return;
309 }
310
311 if (flash_erase(ptn)) {
312 fastboot_fail("failed to erase partition");
313 return;
314 }
315 fastboot_okay("");
316}
317
318void cmd_flash(const char *arg, void *data, unsigned sz)
319{
320 struct ptentry *ptn;
321 struct ptable *ptable;
322 unsigned extra = 0;
323
324 ptable = flash_get_ptable();
325 if (ptable == NULL) {
326 fastboot_fail("partition table doesn't exist");
327 return;
328 }
329
330 ptn = ptable_find(ptable, arg);
331 if (ptn == NULL) {
332 fastboot_fail("unknown partition name");
333 return;
334 }
335
336 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
337 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
338 fastboot_fail("image is not a boot image");
339 return;
340 }
341 }
342
343 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800344 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800345 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800346 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800347
348 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
349 if (flash_write(ptn, extra, data, sz)) {
350 fastboot_fail("flash write failure");
351 return;
352 }
353 dprintf(INFO, "partition '%s' updated\n", ptn->name);
354 fastboot_okay("");
355}
356
357void cmd_continue(const char *arg, void *data, unsigned sz)
358{
359 fastboot_okay("");
360 udc_stop();
361
362 boot_linux_from_flash();
363}
364
Brian Swetland9c4c0752009-01-25 16:23:50 -0800365void aboot_init(const struct app_descriptor *app)
366{
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800367 page_size = flash_page_size();
368 page_mask = page_size - 1;
Dima Zavinb4283602009-01-26 16:36:57 -0800369 if (keys_get_state(KEY_BACK) != 0)
370 goto fastboot;
371
372 boot_linux_from_flash();
373 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
374 "to fastboot mode.\n");
375
376fastboot:
Brian Swetland9c4c0752009-01-25 16:23:50 -0800377 udc_init(&surf_udc_device);
378
379 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800380 fastboot_register("erase:", cmd_erase);
381 fastboot_register("flash:", cmd_flash);
382 fastboot_register("continue", cmd_continue);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800383 fastboot_publish("product", "swordfish");
384 fastboot_publish("kernel", "lk");
385
Brian Swetland2defe162009-08-18 14:35:59 -0700386 fastboot_init((void*) SCRATCH_ADDR, 100 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800387 udc_start();
388}
389
390APP_START(aboot)
391 .init = aboot_init,
392APP_END
393