blob: 42793fc7fb0e4ef25f475decd7ff3fbfd229d425 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <app.h>
33#include <debug.h>
34#include <arch/arm.h>
35#include <dev/udc.h>
36#include <string.h>
37#include <kernel/thread.h>
38#include <arch/ops.h>
39
Dima Zavin214cc642009-01-26 11:16:21 -080040#include <dev/flash.h>
41#include <lib/ptable.h>
42
Brian Swetland9c4c0752009-01-25 16:23:50 -080043#include "bootimg.h"
44#include "fastboot.h"
45
46#define TAGS_ADDR 0x10000100
47#define KERNEL_ADDR 0x10800000
48#define RAMDISK_ADDR 0x11000000
Dima Zavin214cc642009-01-26 11:16:21 -080049#define DEFAULT_CMDLINE "mem=50M console=null";
Brian Swetland9c4c0752009-01-25 16:23:50 -080050
51static struct udc_device surf_udc_device = {
52 .vendor_id = 0x18d1,
53 .product_id = 0x0001,
54 .version_id = 0x0100,
55 .manufacturer = "Google",
56 .product = "Android",
57};
58
59void platform_uninit_timer(void);
60
61
62void boot_linux(void *kernel, unsigned *tags,
63 const char *cmdline, unsigned machtype,
64 void *ramdisk, unsigned ramdisk_size)
65{
66 unsigned *ptr = tags;
67 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
68
69 /* CORE */
70 *ptr++ = 2;
71 *ptr++ = 0x54410001;
72
73 if (ramdisk_size) {
74 *ptr++ = 4;
75 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -080076 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -080077 *ptr++ = ramdisk_size;
78 }
79
80 if (cmdline && cmdline[0]) {
81 unsigned n;
82 /* include terminating 0 and round up to a word multiple */
83 n = (strlen(cmdline) + 4) & (~3);
84 *ptr++ = (n / 4) + 2;
85 *ptr++ = 0x54410009;
86 memcpy(ptr, cmdline, n);
87 ptr += (n / 4);
88 }
89
90 /* END */
91 *ptr++ = 0;
92 *ptr++ = 0;
93
94 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
95 kernel, ramdisk, ramdisk_size);
96 if (cmdline)
97 dprintf(INFO, "cmdline: %s\n", cmdline);
98
99 enter_critical_section();
100 platform_uninit_timer();
101 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800102 arch_disable_mmu();
103
104 entry(0, machtype, tags);
105
106}
107
108#define PAGE_MASK 2047
109
110#define ROUND_TO_PAGE(x) (((x) + PAGE_MASK) & (~PAGE_MASK))
111
Dima Zavin214cc642009-01-26 11:16:21 -0800112static unsigned char buf[2048];
113
114int boot_linux_from_flash(void)
115{
116 struct boot_img_hdr *hdr = (void*) buf;
117 unsigned n;
118 struct ptentry *ptn;
119 struct ptable *ptable;
120 unsigned offset = 0;
121 const char *cmdline;
122
123 ptable = flash_get_ptable();
124 if (ptable == NULL) {
125 dprintf(CRITICAL, "ERROR: Partition table not found\n");
126 return -1;
127 }
128
129 ptn = ptable_find(ptable, "boot");
130 if (ptn == NULL) {
131 dprintf(CRITICAL, "ERROR: No boot partition found\n");
132 return -1;
133 }
134
135 if (flash_read(ptn, offset, buf, 2048)) {
136 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
137 return -1;
138 }
139 offset += 2048;
140
141 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
142 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
143 return -1;
144 }
145
146 n = ROUND_TO_PAGE(hdr->kernel_size);
147 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
148 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
149 return -1;
150 }
151 offset += n;
152
153 n = ROUND_TO_PAGE(hdr->ramdisk_size);
154 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
155 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
156 return -1;
157 }
158 offset += n;
159
160 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
161 hdr->kernel_size);
162 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
163 hdr->ramdisk_size);
164
165 if(hdr->cmdline[0]) {
166 cmdline = (char*) hdr->cmdline;
167 } else {
168 cmdline = DEFAULT_CMDLINE;
169 }
170 dprintf(INFO, "cmdline = '%s'\n", cmdline);
171
172 /* TODO: create/pass atags to kernel */
173
174 dprintf(INFO, "\nBooting Linux\n");
175 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
176 (const char *)cmdline, 1008000,
177 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
178
179 return 0;
180}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800181
182void cmd_boot(const char *arg, void *data, unsigned sz)
183{
184 unsigned kernel_actual;
185 unsigned ramdisk_actual;
186 static struct boot_img_hdr hdr;
187 char *ptr = ((char*) data);
188
189 if (sz < sizeof(hdr)) {
190 fastboot_fail("invalid bootimage header");
191 return;
192 }
193
194 memcpy(&hdr, data, sizeof(hdr));
195
196 /* ensure commandline is terminated */
197 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
198
199 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size);
200 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size);
201
202 if (2048 + kernel_actual + ramdisk_actual < sz) {
203 fastboot_fail("incomplete bootimage");
204 return;
205 }
206
207 memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
208 memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);
209
210 fastboot_okay("");
211 udc_stop();
212
213
214 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
215 (const char*) hdr.cmdline, 1008000,
216 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
217}
218
Dima Zavin214cc642009-01-26 11:16:21 -0800219void cmd_erase(const char *arg, void *data, unsigned sz)
220{
221 struct ptentry *ptn;
222 struct ptable *ptable;
223
224 ptable = flash_get_ptable();
225 if (ptable == NULL) {
226 fastboot_fail("partition table doesn't exist");
227 return;
228 }
229
230 ptn = ptable_find(ptable, arg);
231 if (ptn == NULL) {
232 fastboot_fail("unknown partition name");
233 return;
234 }
235
236 if (flash_erase(ptn)) {
237 fastboot_fail("failed to erase partition");
238 return;
239 }
240 fastboot_okay("");
241}
242
243void cmd_flash(const char *arg, void *data, unsigned sz)
244{
245 struct ptentry *ptn;
246 struct ptable *ptable;
247 unsigned extra = 0;
248
249 ptable = flash_get_ptable();
250 if (ptable == NULL) {
251 fastboot_fail("partition table doesn't exist");
252 return;
253 }
254
255 ptn = ptable_find(ptable, arg);
256 if (ptn == NULL) {
257 fastboot_fail("unknown partition name");
258 return;
259 }
260
261 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
262 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
263 fastboot_fail("image is not a boot image");
264 return;
265 }
266 }
267
268 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
269 extra = 64;
270 else
271 sz = ROUND_TO_PAGE(sz);
272
273 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
274 if (flash_write(ptn, extra, data, sz)) {
275 fastboot_fail("flash write failure");
276 return;
277 }
278 dprintf(INFO, "partition '%s' updated\n", ptn->name);
279 fastboot_okay("");
280}
281
282void cmd_continue(const char *arg, void *data, unsigned sz)
283{
284 fastboot_okay("");
285 udc_stop();
286
287 boot_linux_from_flash();
288}
289
Brian Swetland9c4c0752009-01-25 16:23:50 -0800290void aboot_init(const struct app_descriptor *app)
291{
292 udc_init(&surf_udc_device);
293
294 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800295 fastboot_register("erase:", cmd_erase);
296 fastboot_register("flash:", cmd_flash);
297 fastboot_register("continue", cmd_continue);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800298 fastboot_publish("product", "swordfish");
299 fastboot_publish("kernel", "lk");
300
301 fastboot_init((void*) 0x10100000, 100 * 1024 * 1024);
302 udc_start();
303}
304
305APP_START(aboot)
306 .init = aboot_init,
307APP_END
308