blob: dad3cb6491f595bcd34b9b0ff40d79689b46e2de [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Ajay Dudanid04110c2011-01-17 23:55:07 -08005 * Copyright (c) 2009-2011, 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>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
48#include <platform.h>
Dima Zavin214cc642009-01-26 11:16:21 -080049
Shashank Mittal024c0332010-02-03 11:44:00 -080050#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080051#include "bootimg.h"
52#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070053#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070054#include "mmc.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080055
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070056#include "scm_decrypt.h"
57
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070058#define EXPAND(NAME) #NAME
59#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080060#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070061
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080062#ifdef MEMBASE
63#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
64#else
David Ng183a7422009-12-07 14:55:21 -080065#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080066#endif
67
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080068#define RECOVERY_MODE 0x77665502
69#define FASTBOOT_MODE 0x77665500
70
David Ng183a7422009-12-07 14:55:21 -080071static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080072static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070073static const char *battchg_pause = " androidboot.battchg_pause=true";
David Ng183a7422009-12-07 14:55:21 -080074
Ajay Dudani6cff85e2011-02-04 16:02:16 -080075static const char *baseband_apq = " androidboot.baseband=apq";
76static const char *baseband_msm = " androidboot.baseband=msm";
77static const char *baseband_csfb = " androidboot.baseband=csfb";
78static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080079
Brian Swetland9c4c0752009-01-25 16:23:50 -080080static struct udc_device surf_udc_device = {
81 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080082 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080083 .version_id = 0x0100,
84 .manufacturer = "Google",
85 .product = "Android",
86};
87
Dima Zavin42168f22009-01-30 11:52:22 -080088struct atag_ptbl_entry
89{
90 char name[16];
91 unsigned offset;
92 unsigned size;
93 unsigned flags;
94};
95
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080096char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -070097
Dima Zavin42168f22009-01-30 11:52:22 -080098static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
99{
100 struct atag_ptbl_entry atag_ptn;
101
Greg Griscod6250552011-06-29 14:40:23 -0700102 if (ptn->type == TYPE_MODEM_PARTITION) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800103 return;
Greg Griscod6250552011-06-29 14:40:23 -0700104 }
Dima Zavin42168f22009-01-30 11:52:22 -0800105 memcpy(atag_ptn.name, ptn->name, 16);
106 atag_ptn.name[15] = '\0';
107 atag_ptn.offset = ptn->start;
108 atag_ptn.size = ptn->length;
109 atag_ptn.flags = ptn->flags;
110 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
111 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
112}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800113
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700114void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800115 const char *cmdline, unsigned machtype,
116 void *ramdisk, unsigned ramdisk_size)
117{
118 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800119 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800120 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800121 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800122 int cmdline_len = 0;
123 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700124 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800125
126 /* CORE */
127 *ptr++ = 2;
128 *ptr++ = 0x54410001;
129
130 if (ramdisk_size) {
131 *ptr++ = 4;
132 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800133 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800134 *ptr++ = ramdisk_size;
135 }
136
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800137 ptr = target_atag_mem(ptr);
138
David Ng183a7422009-12-07 14:55:21 -0800139 if (!target_is_emmc_boot()) {
140 /* Skip NAND partition ATAGS for eMMC boot */
141 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
142 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800143 for(i=0; i < ptable->count; i++) {
144 struct ptentry *ptn;
145 ptn = ptable_get(ptable, i);
146 if (ptn->type == TYPE_APPS_PARTITION)
147 pcount++;
148 }
149 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800150 sizeof(unsigned)));
151 *ptr++ = 0x4d534d70;
152 for (i = 0; i < ptable->count; ++i)
153 ptentry_to_tag(&ptr, ptable_get(ptable, i));
154 }
Dima Zavin42168f22009-01-30 11:52:22 -0800155 }
156
Brian Swetland9c4c0752009-01-25 16:23:50 -0800157 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800158 cmdline_len = strlen(cmdline);
159 have_cmdline = 1;
160 }
161 if (target_is_emmc_boot()) {
162 cmdline_len += strlen(emmc_cmdline);
163 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800164
165 cmdline_len += strlen(usb_sn_cmdline);
166 cmdline_len += strlen(sn_buf);
167
David Ngf773dde2010-07-26 19:55:08 -0700168 if (target_pause_for_battery_charge()) {
169 pause_at_bootup = 1;
170 cmdline_len += strlen(battchg_pause);
171 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800172
173 /* Determine correct androidboot.baseband to use */
174 switch(target_baseband())
175 {
176 case BASEBAND_APQ:
177 cmdline_len += strlen(baseband_apq);
178 break;
179
180 case BASEBAND_MSM:
181 cmdline_len += strlen(baseband_msm);
182 break;
183
184 case BASEBAND_CSFB:
185 cmdline_len += strlen(baseband_csfb);
186 break;
187
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800188 case BASEBAND_SVLTE2A:
189 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800190 break;
191 }
192
David Ng183a7422009-12-07 14:55:21 -0800193 if (cmdline_len > 0) {
194 const char *src;
195 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196 unsigned n;
197 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800198 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800199 *ptr++ = (n / 4) + 2;
200 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800201 dst = (char *)ptr;
202 if (have_cmdline) {
203 src = cmdline;
204 while ((*dst++ = *src++));
205 }
206 if (target_is_emmc_boot()) {
207 src = emmc_cmdline;
208 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700209 have_cmdline = 1;
210 while ((*dst++ = *src++));
211 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800212
213 src = usb_sn_cmdline;
214 if (have_cmdline) --dst;
215 have_cmdline = 1;
216 while ((*dst++ = *src++));
217 src = sn_buf;
218 if (have_cmdline) --dst;
219 have_cmdline = 1;
220 while ((*dst++ = *src++));
221
David Ngf773dde2010-07-26 19:55:08 -0700222 if (pause_at_bootup) {
223 src = battchg_pause;
224 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800225 while ((*dst++ = *src++));
226 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800227
228 switch(target_baseband())
229 {
230 case BASEBAND_APQ:
231 src = baseband_apq;
232 if (have_cmdline) --dst;
233 while ((*dst++ = *src++));
234 break;
235
236 case BASEBAND_MSM:
237 src = baseband_msm;
238 if (have_cmdline) --dst;
239 while ((*dst++ = *src++));
240 break;
241
242 case BASEBAND_CSFB:
243 src = baseband_csfb;
244 if (have_cmdline) --dst;
245 while ((*dst++ = *src++));
246 break;
247
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800248 case BASEBAND_SVLTE2A:
249 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800250 if (have_cmdline) --dst;
251 while ((*dst++ = *src++));
252 break;
253 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800254 ptr += (n / 4);
255 }
256
257 /* END */
258 *ptr++ = 0;
259 *ptr++ = 0;
260
261 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
262 kernel, ramdisk, ramdisk_size);
263 if (cmdline)
264 dprintf(INFO, "cmdline: %s\n", cmdline);
265
266 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700267 /* do any platform specific cleanup before kernel entry */
268 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800269 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800270 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800271 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800272}
273
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800274unsigned page_size = 0;
275unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800276
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800277#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800278
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800279static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800280
Shashank Mittal23b8f422010-04-16 19:27:21 -0700281int boot_linux_from_mmc(void)
282{
283 struct boot_img_hdr *hdr = (void*) buf;
284 struct boot_img_hdr *uhdr;
285 unsigned offset = 0;
286 unsigned long long ptn = 0;
287 unsigned n = 0;
288 const char *cmdline;
289
290 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
291 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
292 dprintf(INFO, "Unified boot method!\n");
293 hdr = uhdr;
294 goto unified_boot;
295 }
Greg Griscod6250552011-06-29 14:40:23 -0700296 if (!boot_into_recovery) {
297 ptn = mmc_ptn_offset((unsigned char *) "boot");
298 if (ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700299 dprintf(CRITICAL, "ERROR: No boot partition found\n");
300 return -1;
301 }
Greg Griscod6250552011-06-29 14:40:23 -0700302 } else {
303 ptn = mmc_ptn_offset((unsigned char *) "recovery");
304 if (ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700305 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
306 return -1;
307 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700308 }
309
Greg Griscod6250552011-06-29 14:40:23 -0700310 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700311 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
312 return -1;
313 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700314
315 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700316 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700317 return -1;
318 }
319
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700320 if (hdr->page_size && (hdr->page_size != page_size)) {
321 page_size = hdr->page_size;
322 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700323 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700324 offset += page_size;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700325
326 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
327 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
328 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
329 return -1;
330 }
331 offset += n;
332
333 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800334 if(n != 0)
335 {
336 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
337 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
338 return -1;
339 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700340 }
341 offset += n;
342
343unified_boot:
344 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
345 hdr->kernel_size);
346 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
347 hdr->ramdisk_size);
348
349 if(hdr->cmdline[0]) {
350 cmdline = (char*) hdr->cmdline;
351 } else {
352 cmdline = DEFAULT_CMDLINE;
353 }
354 dprintf(INFO, "cmdline = '%s'\n", cmdline);
355
356 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700357 boot_linux((void *)hdr->kernel_addr, hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700358 (const char *)cmdline, board_machtype(),
359 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
360
361 return 0;
362}
363
Dima Zavin214cc642009-01-26 11:16:21 -0800364int boot_linux_from_flash(void)
365{
366 struct boot_img_hdr *hdr = (void*) buf;
367 unsigned n;
368 struct ptentry *ptn;
369 struct ptable *ptable;
370 unsigned offset = 0;
371 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800372
David Ng183a7422009-12-07 14:55:21 -0800373 if (target_is_emmc_boot()) {
374 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
375 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
376 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
377 return -1;
378 }
379 goto continue_boot;
380 }
381
Dima Zavin214cc642009-01-26 11:16:21 -0800382 ptable = flash_get_ptable();
383 if (ptable == NULL) {
384 dprintf(CRITICAL, "ERROR: Partition table not found\n");
385 return -1;
386 }
387
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800388 if(!boot_into_recovery)
389 {
390 ptn = ptable_find(ptable, "boot");
391 if (ptn == NULL) {
392 dprintf(CRITICAL, "ERROR: No boot partition found\n");
393 return -1;
394 }
395 }
396 else
397 {
398 ptn = ptable_find(ptable, "recovery");
399 if (ptn == NULL) {
400 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
401 return -1;
402 }
Dima Zavin214cc642009-01-26 11:16:21 -0800403 }
404
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800405 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800406 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
407 return -1;
408 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800409 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800410
411 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700412 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800413 return -1;
414 }
415
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800416 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700417 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800418 return -1;
419 }
420
421 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800422 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
423 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
424 return -1;
425 }
426 offset += n;
427
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800428 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800429 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
430 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
431 return -1;
432 }
433 offset += n;
434
David Ng183a7422009-12-07 14:55:21 -0800435continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800436 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
437 hdr->kernel_size);
438 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
439 hdr->ramdisk_size);
440
441 if(hdr->cmdline[0]) {
442 cmdline = (char*) hdr->cmdline;
443 } else {
444 cmdline = DEFAULT_CMDLINE;
445 }
446 dprintf(INFO, "cmdline = '%s'\n", cmdline);
447
448 /* TODO: create/pass atags to kernel */
449
450 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700451 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800452 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800453 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
454
455 return 0;
456}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800457
458void cmd_boot(const char *arg, void *data, unsigned sz)
459{
460 unsigned kernel_actual;
461 unsigned ramdisk_actual;
462 static struct boot_img_hdr hdr;
463 char *ptr = ((char*) data);
464
465 if (sz < sizeof(hdr)) {
466 fastboot_fail("invalid bootimage header");
467 return;
468 }
469
470 memcpy(&hdr, data, sizeof(hdr));
471
472 /* ensure commandline is terminated */
473 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
474
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700475 if(target_is_emmc_boot() && hdr.page_size) {
476 page_size = hdr.page_size;
477 page_mask = page_size - 1;
478 }
479
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800480 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
481 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800482
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700483 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800484 fastboot_fail("incomplete bootimage");
485 return;
486 }
487
Ajay Dudanie28a6072011-07-01 13:59:46 -0700488 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
489 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800490
491 fastboot_okay("");
492 udc_stop();
493
Ajay Dudanie28a6072011-07-01 13:59:46 -0700494 boot_linux((void*) hdr.kernel_addr, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800495 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700496 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800497}
498
Dima Zavin214cc642009-01-26 11:16:21 -0800499void cmd_erase(const char *arg, void *data, unsigned sz)
500{
501 struct ptentry *ptn;
502 struct ptable *ptable;
503
504 ptable = flash_get_ptable();
505 if (ptable == NULL) {
506 fastboot_fail("partition table doesn't exist");
507 return;
508 }
509
510 ptn = ptable_find(ptable, arg);
511 if (ptn == NULL) {
512 fastboot_fail("unknown partition name");
513 return;
514 }
515
516 if (flash_erase(ptn)) {
517 fastboot_fail("failed to erase partition");
518 return;
519 }
520 fastboot_okay("");
521}
522
Bikas Gurungd48bd242010-09-04 19:54:32 -0700523
524void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
525{
526 unsigned long long ptn = 0;
527 unsigned int out[512] = {0};
528
Greg Griscod6250552011-06-29 14:40:23 -0700529 ptn = mmc_ptn_offset((unsigned char *) arg);
530 if (ptn == 0) {
Bikas Gurungd48bd242010-09-04 19:54:32 -0700531 fastboot_fail("partition table doesn't exist");
532 return;
533 }
534
535
536 /* Simple inefficient version of erase. Just writing
537 0 in first block */
538 if (mmc_write(ptn , 512, (unsigned int *)out)) {
539 fastboot_fail("failed to erase partition");
540 return;
541 }
542 fastboot_okay("");
543}
544
545
Ajay Dudani5c761132011-04-07 20:19:04 -0700546void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700547{
548 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700549 unsigned long long size = 0;
550
Greg Grisco6e754772011-06-23 12:19:39 -0700551 if (!strcmp(arg, "partition"))
552 {
553 dprintf(INFO, "Attempt to write partition image.\n");
554 if (mmc_write_partition(sz, (unsigned char *) data)) {
555 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700556 return;
557 }
558 }
Greg Grisco6e754772011-06-23 12:19:39 -0700559 else
560 {
561 ptn = mmc_ptn_offset((unsigned char *) arg);
562 if(ptn == 0) {
563 fastboot_fail("partition table doesn't exist");
564 return;
565 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700566
Greg Grisco6e754772011-06-23 12:19:39 -0700567 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
568 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
569 fastboot_fail("image is not a boot image");
570 return;
571 }
572 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700573
Greg Grisco6e754772011-06-23 12:19:39 -0700574 size = mmc_ptn_size((unsigned char *) arg);
575 if (ROUND_TO_PAGE(sz,511) > size) {
576 fastboot_fail("size too large");
577 return;
578 }
579 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
580 fastboot_fail("flash write failure");
581 return;
582 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700583 }
584 fastboot_okay("");
585 return;
586}
587
Greg Griscod6250552011-06-29 14:40:23 -0700588
589
Ajay Dudani5c761132011-04-07 20:19:04 -0700590void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
591{
592 unsigned int chunk;
593 unsigned int chunk_data_sz;
594 sparse_header_t *sparse_header;
595 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700596 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700597 unsigned long long ptn = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700598
Greg Griscod6250552011-06-29 14:40:23 -0700599 ptn = mmc_ptn_offset((unsigned char *) arg);
600 if (ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700601 fastboot_fail("partition table doesn't exist");
602 return;
603 }
604
605 /* Read and skip over sparse image header */
606 sparse_header = (sparse_header_t *) data;
607 data += sparse_header->file_hdr_sz;
608 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
609 {
610 /* Skip the remaining bytes in a header that is longer than
611 * we expected.
612 */
613 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
614 }
615
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700616 dprintf (SPEW, "=== Sparse Image Header ===\n");
617 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
618 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
619 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
620 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
621 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
622 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
623 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
624 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700625
626 /* Start processing chunks */
627 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
628 {
629 /* Read and skip over chunk header */
630 chunk_header = (chunk_header_t *) data;
631 data += sizeof(chunk_header_t);
632
633 dprintf (SPEW, "=== Chunk Header ===\n");
634 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
635 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
636 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
637
638 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
639 {
640 /* Skip the remaining bytes in a header that is longer than
641 * we expected.
642 */
643 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
644 }
645
646 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
647 switch (chunk_header->chunk_type)
648 {
649 case CHUNK_TYPE_RAW:
650 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
651 chunk_data_sz))
652 {
653 fastboot_fail("Bogus chunk size for chunk type Raw");
654 return;
655 }
656
Ajay Dudaniab18f022011-05-12 14:39:22 -0700657 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
658 chunk_data_sz,
659 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700660 {
661 fastboot_fail("flash write failure");
662 return;
663 }
664 total_blocks += chunk_header->chunk_sz;
665 data += chunk_data_sz;
666 break;
667
668 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700669 total_blocks += chunk_header->chunk_sz;
670 break;
671
Ajay Dudani5c761132011-04-07 20:19:04 -0700672 case CHUNK_TYPE_CRC:
673 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
674 {
675 fastboot_fail("Bogus chunk size for chunk type Dont Care");
676 return;
677 }
678 total_blocks += chunk_header->chunk_sz;
679 data += chunk_data_sz;
680 break;
681
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700682 default:
Ajay Dudani5c761132011-04-07 20:19:04 -0700683 fastboot_fail("Unknown chunk type");
684 return;
685 }
686 }
687
Ajay Dudani0c6927b2011-05-18 11:12:16 -0700688 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
689 total_blocks, sparse_header->total_blks);
690
691 if(total_blocks != sparse_header->total_blks)
692 {
693 fastboot_fail("sparse image write failure");
694 }
Ajay Dudani5c761132011-04-07 20:19:04 -0700695
696 fastboot_okay("");
697 return;
698}
699
700void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
701{
702 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700703 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
704 unsigned int *magic_number = (unsigned int *) data;
705 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700706
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700707 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
708 magic_number[1] == SSD_HEADER_MAGIC_1)
709 {
710#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -0700711 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700712#endif
Greg Griscod6250552011-06-29 14:40:23 -0700713 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700714 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
715 return;
716 }
717 }
718 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -0700719 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
720 cmd_flash_mmc_img(arg, data, sz);
721 else
722 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -0700723 return;
724}
725
Dima Zavin214cc642009-01-26 11:16:21 -0800726void cmd_flash(const char *arg, void *data, unsigned sz)
727{
728 struct ptentry *ptn;
729 struct ptable *ptable;
730 unsigned extra = 0;
731
732 ptable = flash_get_ptable();
733 if (ptable == NULL) {
734 fastboot_fail("partition table doesn't exist");
735 return;
736 }
737
738 ptn = ptable_find(ptable, arg);
739 if (ptn == NULL) {
740 fastboot_fail("unknown partition name");
741 return;
742 }
743
744 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
745 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
746 fastboot_fail("image is not a boot image");
747 return;
748 }
749 }
750
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800751 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +0530752 || !strcmp(ptn->name, "persist")) {
753 if (flash_ecc_bch_enabled())
754 /* Spare data bytes for 8 bit ECC increased by 4 */
755 extra = ((page_size >> 9) * 20);
756 else
757 extra = ((page_size >> 9) * 16);
758 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800759 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800760
761 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
762 if (flash_write(ptn, extra, data, sz)) {
763 fastboot_fail("flash write failure");
764 return;
765 }
766 dprintf(INFO, "partition '%s' updated\n", ptn->name);
767 fastboot_okay("");
768}
769
770void cmd_continue(const char *arg, void *data, unsigned sz)
771{
772 fastboot_okay("");
773 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700774 if (target_is_emmc_boot())
775 {
776 boot_linux_from_mmc();
777 }
778 else
779 {
780 boot_linux_from_flash();
781 }
Dima Zavin214cc642009-01-26 11:16:21 -0800782}
783
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800784void cmd_reboot(const char *arg, void *data, unsigned sz)
785{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700786 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800787 fastboot_okay("");
788 reboot_device(0);
789}
790
791void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
792{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700793 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800794 fastboot_okay("");
795 reboot_device(FASTBOOT_MODE);
796}
797
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700798void splash_screen ()
799{
800 struct ptentry *ptn;
801 struct ptable *ptable;
802 struct fbcon_config *fb_display = NULL;
803
804 if (!target_is_emmc_boot())
805 {
806 ptable = flash_get_ptable();
807 if (ptable == NULL) {
808 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -0700809 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700810 }
811
812 ptn = ptable_find(ptable, "splash");
813 if (ptn == NULL) {
814 dprintf(CRITICAL, "ERROR: No splash partition found\n");
815 } else {
816 fb_display = fbcon_display();
817 if (fb_display) {
818 if (flash_read(ptn, 0, fb_display->base,
819 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
820 fbcon_clear();
821 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
822 }
823 }
824 }
825 }
826}
827
Brian Swetland9c4c0752009-01-25 16:23:50 -0800828void aboot_init(const struct app_descriptor *app)
829{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800830 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700831 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700832 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700833
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700834 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700835 if (target_is_emmc_boot())
836 {
837 page_size = 2048;
838 page_mask = page_size - 1;
839 }
840 else
841 {
842 page_size = flash_page_size();
843 page_mask = page_size - 1;
844 }
845
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700846 /* Display splash screen if enabled */
847 #if DISPLAY_SPLASH_SCREEN
848 display_init();
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700849 dprintf(SPEW, "Diplay initialized\n");
Greg Griscod6250552011-06-29 14:40:23 -0700850 display_image_on_screen();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700851 #endif
852
Greg Griscod6250552011-06-29 14:40:23 -0700853 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700854 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800855 surf_udc_device.serialno = sn_buf;
856
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700857 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700858 if (keys_get_state(KEY_HOME) != 0)
859 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500860 if (keys_get_state(KEY_VOLUMEUP) != 0)
861 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700862 if(!boot_into_recovery)
863 {
864 if (keys_get_state(KEY_BACK) != 0)
865 goto fastboot;
866 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
867 goto fastboot;
868 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700869
870 #if NO_KEYPAD_DRIVER
871 /* With no keypad implementation, check the status of USB connection. */
872 /* If USB is connected then go into fastboot mode. */
873 usb_init = 1;
874 udc_init(&surf_udc_device);
875 if (usb_cable_status())
876 goto fastboot;
877 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700878
Ajay Dudani77421292010-10-27 19:34:06 -0700879 reboot_mode = check_reboot_mode();
880 if (reboot_mode == RECOVERY_MODE) {
881 boot_into_recovery = 1;
882 } else if(reboot_mode == FASTBOOT_MODE) {
883 goto fastboot;
884 }
885
Shashank Mittal23b8f422010-04-16 19:27:21 -0700886 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700887 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700888 if(emmc_recovery_init())
889 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700890 boot_linux_from_mmc();
891 }
892 else
893 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700894 recovery_init();
895 boot_linux_from_flash();
896 }
Dima Zavinb4283602009-01-26 16:36:57 -0800897 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
898 "to fastboot mode.\n");
899
900fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700901
Amol Jadi57abe4c2011-05-24 15:47:27 -0700902 target_fastboot_init();
903
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700904 if(!usb_init)
905 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800906
907 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700908
Shashank Mittal23b8f422010-04-16 19:27:21 -0700909 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700910 {
911 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700912 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700913 }
914 else
915 {
916 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700917 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700918 }
919
920 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800921 fastboot_register("reboot", cmd_reboot);
922 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700923 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800924 fastboot_publish("kernel", "lk");
Greg Grisco6e754772011-06-23 12:19:39 -0700925 mmc_dump_partition_info();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700926 sz = target_get_max_flash_size();
927 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800928 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800929}
930
931APP_START(aboot)
932 .init = aboot_init,
933APP_END
934