blob: dbfc0acc0361ade1bfd447807482b2f4c0671bff [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
Brian Swetland9c4c0752009-01-25 16:23:50 -080050static struct udc_device surf_udc_device = {
51 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080052 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080053 .version_id = 0x0100,
54 .manufacturer = "Google",
55 .product = "Android",
56};
57
Dima Zavin42168f22009-01-30 11:52:22 -080058struct atag_ptbl_entry
59{
60 char name[16];
61 unsigned offset;
62 unsigned size;
63 unsigned flags;
64};
65
Brian Swetland9c4c0752009-01-25 16:23:50 -080066void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080067unsigned* target_atag_mem(unsigned* ptr);
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Dima Zavin42168f22009-01-30 11:52:22 -080069static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
70{
71 struct atag_ptbl_entry atag_ptn;
72
73 memcpy(atag_ptn.name, ptn->name, 16);
74 atag_ptn.name[15] = '\0';
75 atag_ptn.offset = ptn->start;
76 atag_ptn.size = ptn->length;
77 atag_ptn.flags = ptn->flags;
78 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
79 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
80}
Brian Swetland9c4c0752009-01-25 16:23:50 -080081
82void boot_linux(void *kernel, unsigned *tags,
83 const char *cmdline, unsigned machtype,
84 void *ramdisk, unsigned ramdisk_size)
85{
86 unsigned *ptr = tags;
87 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -080088 struct ptable *ptable;
Brian Swetland9c4c0752009-01-25 16:23:50 -080089
90 /* CORE */
91 *ptr++ = 2;
92 *ptr++ = 0x54410001;
93
94 if (ramdisk_size) {
95 *ptr++ = 4;
96 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -080097 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -080098 *ptr++ = ramdisk_size;
99 }
100
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800101 ptr = target_atag_mem(ptr);
102
Dima Zavin42168f22009-01-30 11:52:22 -0800103 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
104 int i;
105 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
106 sizeof(unsigned)));
107 *ptr++ = 0x4d534d70;
108 for (i = 0; i < ptable->count; ++i)
109 ptentry_to_tag(&ptr, ptable_get(ptable, i));
110 }
111
Brian Swetland9c4c0752009-01-25 16:23:50 -0800112 if (cmdline && cmdline[0]) {
113 unsigned n;
114 /* include terminating 0 and round up to a word multiple */
115 n = (strlen(cmdline) + 4) & (~3);
116 *ptr++ = (n / 4) + 2;
117 *ptr++ = 0x54410009;
118 memcpy(ptr, cmdline, n);
119 ptr += (n / 4);
120 }
121
122 /* END */
123 *ptr++ = 0;
124 *ptr++ = 0;
125
126 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
127 kernel, ramdisk, ramdisk_size);
128 if (cmdline)
129 dprintf(INFO, "cmdline: %s\n", cmdline);
130
131 enter_critical_section();
132 platform_uninit_timer();
133 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800134 arch_disable_mmu();
135
136 entry(0, machtype, tags);
137
138}
139
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800140unsigned page_size = 0;
141unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800142
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800143#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800144
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800145static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800146
147int boot_linux_from_flash(void)
148{
149 struct boot_img_hdr *hdr = (void*) buf;
150 unsigned n;
151 struct ptentry *ptn;
152 struct ptable *ptable;
153 unsigned offset = 0;
154 const char *cmdline;
155
156 ptable = flash_get_ptable();
157 if (ptable == NULL) {
158 dprintf(CRITICAL, "ERROR: Partition table not found\n");
159 return -1;
160 }
161
162 ptn = ptable_find(ptable, "boot");
163 if (ptn == NULL) {
164 dprintf(CRITICAL, "ERROR: No boot partition found\n");
165 return -1;
166 }
167
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800168 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800169 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
170 return -1;
171 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800172 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800173
174 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
175 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
176 return -1;
177 }
178
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800179 if (hdr->page_size != page_size) {
180 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
181 return -1;
182 }
183
184 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800185 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
186 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
187 return -1;
188 }
189 offset += n;
190
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800191 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800192 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
193 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
194 return -1;
195 }
196 offset += n;
197
198 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
199 hdr->kernel_size);
200 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
201 hdr->ramdisk_size);
202
203 if(hdr->cmdline[0]) {
204 cmdline = (char*) hdr->cmdline;
205 } else {
206 cmdline = DEFAULT_CMDLINE;
207 }
208 dprintf(INFO, "cmdline = '%s'\n", cmdline);
209
210 /* TODO: create/pass atags to kernel */
211
212 dprintf(INFO, "\nBooting Linux\n");
213 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Dima Zavinf9faf3a2009-01-29 11:43:30 -0800214 (const char *)cmdline, LINUX_MACHTYPE,
Dima Zavin214cc642009-01-26 11:16:21 -0800215 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
216
217 return 0;
218}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800219
220void cmd_boot(const char *arg, void *data, unsigned sz)
221{
222 unsigned kernel_actual;
223 unsigned ramdisk_actual;
224 static struct boot_img_hdr hdr;
225 char *ptr = ((char*) data);
226
227 if (sz < sizeof(hdr)) {
228 fastboot_fail("invalid bootimage header");
229 return;
230 }
231
232 memcpy(&hdr, data, sizeof(hdr));
233
234 /* ensure commandline is terminated */
235 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
236
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800237 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
238 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800239
240 if (2048 + kernel_actual + ramdisk_actual < sz) {
241 fastboot_fail("incomplete bootimage");
242 return;
243 }
244
245 memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
246 memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);
247
248 fastboot_okay("");
249 udc_stop();
250
251
252 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Dima Zavinf9faf3a2009-01-29 11:43:30 -0800253 (const char*) hdr.cmdline, LINUX_MACHTYPE,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800254 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
255}
256
Dima Zavin214cc642009-01-26 11:16:21 -0800257void cmd_erase(const char *arg, void *data, unsigned sz)
258{
259 struct ptentry *ptn;
260 struct ptable *ptable;
261
262 ptable = flash_get_ptable();
263 if (ptable == NULL) {
264 fastboot_fail("partition table doesn't exist");
265 return;
266 }
267
268 ptn = ptable_find(ptable, arg);
269 if (ptn == NULL) {
270 fastboot_fail("unknown partition name");
271 return;
272 }
273
274 if (flash_erase(ptn)) {
275 fastboot_fail("failed to erase partition");
276 return;
277 }
278 fastboot_okay("");
279}
280
281void cmd_flash(const char *arg, void *data, unsigned sz)
282{
283 struct ptentry *ptn;
284 struct ptable *ptable;
285 unsigned extra = 0;
286
287 ptable = flash_get_ptable();
288 if (ptable == NULL) {
289 fastboot_fail("partition table doesn't exist");
290 return;
291 }
292
293 ptn = ptable_find(ptable, arg);
294 if (ptn == NULL) {
295 fastboot_fail("unknown partition name");
296 return;
297 }
298
299 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
300 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
301 fastboot_fail("image is not a boot image");
302 return;
303 }
304 }
305
306 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800307 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800308 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800309 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800310
311 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
312 if (flash_write(ptn, extra, data, sz)) {
313 fastboot_fail("flash write failure");
314 return;
315 }
316 dprintf(INFO, "partition '%s' updated\n", ptn->name);
317 fastboot_okay("");
318}
319
320void cmd_continue(const char *arg, void *data, unsigned sz)
321{
322 fastboot_okay("");
323 udc_stop();
324
325 boot_linux_from_flash();
326}
327
Brian Swetland9c4c0752009-01-25 16:23:50 -0800328void aboot_init(const struct app_descriptor *app)
329{
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800330 page_size = flash_page_size();
331 page_mask = page_size - 1;
Dima Zavinb4283602009-01-26 16:36:57 -0800332 if (keys_get_state(KEY_BACK) != 0)
333 goto fastboot;
334
335 boot_linux_from_flash();
336 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
337 "to fastboot mode.\n");
338
339fastboot:
Brian Swetland9c4c0752009-01-25 16:23:50 -0800340 udc_init(&surf_udc_device);
341
342 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800343 fastboot_register("erase:", cmd_erase);
344 fastboot_register("flash:", cmd_flash);
345 fastboot_register("continue", cmd_continue);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800346 fastboot_publish("product", "swordfish");
347 fastboot_publish("kernel", "lk");
348
Brian Swetland2defe162009-08-18 14:35:59 -0700349 fastboot_init((void*) SCRATCH_ADDR, 100 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800350 udc_start();
351}
352
353APP_START(aboot)
354 .init = aboot_init,
355APP_END
356