blob: 9739379ad769905a7b535f5fe033e0a309155296 [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
Greg Griscod2471ef2011-07-14 13:00:42 -070098extern int emmc_recovery_init(void);
99
100
101
Dima Zavin42168f22009-01-30 11:52:22 -0800102static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
103{
104 struct atag_ptbl_entry atag_ptn;
105
Greg Griscod6250552011-06-29 14:40:23 -0700106 if (ptn->type == TYPE_MODEM_PARTITION) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800107 return;
Greg Griscod6250552011-06-29 14:40:23 -0700108 }
Dima Zavin42168f22009-01-30 11:52:22 -0800109 memcpy(atag_ptn.name, ptn->name, 16);
110 atag_ptn.name[15] = '\0';
111 atag_ptn.offset = ptn->start;
112 atag_ptn.size = ptn->length;
113 atag_ptn.flags = ptn->flags;
114 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
115 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
116}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800117
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700118void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800119 const char *cmdline, unsigned machtype,
120 void *ramdisk, unsigned ramdisk_size)
121{
122 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800123 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800124 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800125 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800126 int cmdline_len = 0;
127 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700128 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129
130 /* CORE */
131 *ptr++ = 2;
132 *ptr++ = 0x54410001;
133
134 if (ramdisk_size) {
135 *ptr++ = 4;
136 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800137 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800138 *ptr++ = ramdisk_size;
139 }
140
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800141 ptr = target_atag_mem(ptr);
142
David Ng183a7422009-12-07 14:55:21 -0800143 if (!target_is_emmc_boot()) {
144 /* Skip NAND partition ATAGS for eMMC boot */
145 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
146 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800147 for(i=0; i < ptable->count; i++) {
148 struct ptentry *ptn;
149 ptn = ptable_get(ptable, i);
150 if (ptn->type == TYPE_APPS_PARTITION)
151 pcount++;
152 }
153 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800154 sizeof(unsigned)));
155 *ptr++ = 0x4d534d70;
156 for (i = 0; i < ptable->count; ++i)
157 ptentry_to_tag(&ptr, ptable_get(ptable, i));
158 }
Dima Zavin42168f22009-01-30 11:52:22 -0800159 }
160
Brian Swetland9c4c0752009-01-25 16:23:50 -0800161 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800162 cmdline_len = strlen(cmdline);
163 have_cmdline = 1;
164 }
165 if (target_is_emmc_boot()) {
166 cmdline_len += strlen(emmc_cmdline);
167 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800168
169 cmdline_len += strlen(usb_sn_cmdline);
170 cmdline_len += strlen(sn_buf);
171
David Ngf773dde2010-07-26 19:55:08 -0700172 if (target_pause_for_battery_charge()) {
173 pause_at_bootup = 1;
174 cmdline_len += strlen(battchg_pause);
175 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800176
177 /* Determine correct androidboot.baseband to use */
178 switch(target_baseband())
179 {
180 case BASEBAND_APQ:
181 cmdline_len += strlen(baseband_apq);
182 break;
183
184 case BASEBAND_MSM:
185 cmdline_len += strlen(baseband_msm);
186 break;
187
188 case BASEBAND_CSFB:
189 cmdline_len += strlen(baseband_csfb);
190 break;
191
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800192 case BASEBAND_SVLTE2A:
193 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800194 break;
195 }
196
David Ng183a7422009-12-07 14:55:21 -0800197 if (cmdline_len > 0) {
198 const char *src;
199 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800200 unsigned n;
201 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800202 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800203 *ptr++ = (n / 4) + 2;
204 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800205 dst = (char *)ptr;
206 if (have_cmdline) {
207 src = cmdline;
208 while ((*dst++ = *src++));
209 }
210 if (target_is_emmc_boot()) {
211 src = emmc_cmdline;
212 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700213 have_cmdline = 1;
214 while ((*dst++ = *src++));
215 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800216
217 src = usb_sn_cmdline;
218 if (have_cmdline) --dst;
219 have_cmdline = 1;
220 while ((*dst++ = *src++));
221 src = sn_buf;
222 if (have_cmdline) --dst;
223 have_cmdline = 1;
224 while ((*dst++ = *src++));
225
David Ngf773dde2010-07-26 19:55:08 -0700226 if (pause_at_bootup) {
227 src = battchg_pause;
228 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800229 while ((*dst++ = *src++));
230 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800231
232 switch(target_baseband())
233 {
234 case BASEBAND_APQ:
235 src = baseband_apq;
236 if (have_cmdline) --dst;
237 while ((*dst++ = *src++));
238 break;
239
240 case BASEBAND_MSM:
241 src = baseband_msm;
242 if (have_cmdline) --dst;
243 while ((*dst++ = *src++));
244 break;
245
246 case BASEBAND_CSFB:
247 src = baseband_csfb;
248 if (have_cmdline) --dst;
249 while ((*dst++ = *src++));
250 break;
251
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800252 case BASEBAND_SVLTE2A:
253 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800254 if (have_cmdline) --dst;
255 while ((*dst++ = *src++));
256 break;
257 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800258 ptr += (n / 4);
259 }
260
261 /* END */
262 *ptr++ = 0;
263 *ptr++ = 0;
264
265 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
266 kernel, ramdisk, ramdisk_size);
267 if (cmdline)
268 dprintf(INFO, "cmdline: %s\n", cmdline);
269
270 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700271 /* do any platform specific cleanup before kernel entry */
272 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800273 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800274 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800275 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800276}
277
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800278unsigned page_size = 0;
279unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800280
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800281#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800282
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800283static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800284
Shashank Mittal23b8f422010-04-16 19:27:21 -0700285int boot_linux_from_mmc(void)
286{
287 struct boot_img_hdr *hdr = (void*) buf;
288 struct boot_img_hdr *uhdr;
289 unsigned offset = 0;
290 unsigned long long ptn = 0;
291 unsigned n = 0;
292 const char *cmdline;
293
294 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
295 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
296 dprintf(INFO, "Unified boot method!\n");
297 hdr = uhdr;
298 goto unified_boot;
299 }
Greg Griscod6250552011-06-29 14:40:23 -0700300 if (!boot_into_recovery) {
301 ptn = mmc_ptn_offset((unsigned char *) "boot");
302 if (ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700303 dprintf(CRITICAL, "ERROR: No boot partition found\n");
304 return -1;
305 }
Greg Griscod6250552011-06-29 14:40:23 -0700306 } else {
307 ptn = mmc_ptn_offset((unsigned char *) "recovery");
308 if (ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700309 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
310 return -1;
311 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700312 }
313
Greg Griscod6250552011-06-29 14:40:23 -0700314 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700315 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
316 return -1;
317 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700318
319 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700320 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700321 return -1;
322 }
323
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700324 if (hdr->page_size && (hdr->page_size != page_size)) {
325 page_size = hdr->page_size;
326 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700327 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700328 offset += page_size;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700329
330 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
331 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
332 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
333 return -1;
334 }
335 offset += n;
336
337 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800338 if(n != 0)
339 {
340 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
341 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
342 return -1;
343 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700344 }
345 offset += n;
346
347unified_boot:
348 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
349 hdr->kernel_size);
350 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
351 hdr->ramdisk_size);
352
353 if(hdr->cmdline[0]) {
354 cmdline = (char*) hdr->cmdline;
355 } else {
356 cmdline = DEFAULT_CMDLINE;
357 }
358 dprintf(INFO, "cmdline = '%s'\n", cmdline);
359
360 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700361 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700362 (const char *)cmdline, board_machtype(),
363 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
364
365 return 0;
366}
367
Dima Zavin214cc642009-01-26 11:16:21 -0800368int boot_linux_from_flash(void)
369{
370 struct boot_img_hdr *hdr = (void*) buf;
371 unsigned n;
372 struct ptentry *ptn;
373 struct ptable *ptable;
374 unsigned offset = 0;
375 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800376
David Ng183a7422009-12-07 14:55:21 -0800377 if (target_is_emmc_boot()) {
378 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
379 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
380 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
381 return -1;
382 }
383 goto continue_boot;
384 }
385
Dima Zavin214cc642009-01-26 11:16:21 -0800386 ptable = flash_get_ptable();
387 if (ptable == NULL) {
388 dprintf(CRITICAL, "ERROR: Partition table not found\n");
389 return -1;
390 }
391
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800392 if(!boot_into_recovery)
393 {
394 ptn = ptable_find(ptable, "boot");
395 if (ptn == NULL) {
396 dprintf(CRITICAL, "ERROR: No boot partition found\n");
397 return -1;
398 }
399 }
400 else
401 {
402 ptn = ptable_find(ptable, "recovery");
403 if (ptn == NULL) {
404 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
405 return -1;
406 }
Dima Zavin214cc642009-01-26 11:16:21 -0800407 }
408
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800409 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800410 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
411 return -1;
412 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800413 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800414
415 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700416 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800417 return -1;
418 }
419
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800420 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700421 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 -0800422 return -1;
423 }
424
425 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800426 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
427 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
428 return -1;
429 }
430 offset += n;
431
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800432 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800433 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
434 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
435 return -1;
436 }
437 offset += n;
438
David Ng183a7422009-12-07 14:55:21 -0800439continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800440 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
441 hdr->kernel_size);
442 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
443 hdr->ramdisk_size);
444
445 if(hdr->cmdline[0]) {
446 cmdline = (char*) hdr->cmdline;
447 } else {
448 cmdline = DEFAULT_CMDLINE;
449 }
450 dprintf(INFO, "cmdline = '%s'\n", cmdline);
451
452 /* TODO: create/pass atags to kernel */
453
454 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700455 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800456 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800457 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
458
459 return 0;
460}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800461
462void cmd_boot(const char *arg, void *data, unsigned sz)
463{
464 unsigned kernel_actual;
465 unsigned ramdisk_actual;
466 static struct boot_img_hdr hdr;
467 char *ptr = ((char*) data);
468
469 if (sz < sizeof(hdr)) {
470 fastboot_fail("invalid bootimage header");
471 return;
472 }
473
474 memcpy(&hdr, data, sizeof(hdr));
475
476 /* ensure commandline is terminated */
477 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
478
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700479 if(target_is_emmc_boot() && hdr.page_size) {
480 page_size = hdr.page_size;
481 page_mask = page_size - 1;
482 }
483
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800484 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
485 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800486
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700487 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 fastboot_fail("incomplete bootimage");
489 return;
490 }
491
Ajay Dudanie28a6072011-07-01 13:59:46 -0700492 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
493 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800494
495 fastboot_okay("");
496 udc_stop();
497
Amol Jadie67872e2011-06-27 14:14:11 -0700498 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800499 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700500 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800501}
502
Dima Zavin214cc642009-01-26 11:16:21 -0800503void cmd_erase(const char *arg, void *data, unsigned sz)
504{
505 struct ptentry *ptn;
506 struct ptable *ptable;
507
508 ptable = flash_get_ptable();
509 if (ptable == NULL) {
510 fastboot_fail("partition table doesn't exist");
511 return;
512 }
513
514 ptn = ptable_find(ptable, arg);
515 if (ptn == NULL) {
516 fastboot_fail("unknown partition name");
517 return;
518 }
519
520 if (flash_erase(ptn)) {
521 fastboot_fail("failed to erase partition");
522 return;
523 }
524 fastboot_okay("");
525}
526
Bikas Gurungd48bd242010-09-04 19:54:32 -0700527
528void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
529{
530 unsigned long long ptn = 0;
531 unsigned int out[512] = {0};
532
Greg Griscod6250552011-06-29 14:40:23 -0700533 ptn = mmc_ptn_offset((unsigned char *) arg);
534 if (ptn == 0) {
Bikas Gurungd48bd242010-09-04 19:54:32 -0700535 fastboot_fail("partition table doesn't exist");
536 return;
537 }
538
539
540 /* Simple inefficient version of erase. Just writing
541 0 in first block */
542 if (mmc_write(ptn , 512, (unsigned int *)out)) {
543 fastboot_fail("failed to erase partition");
544 return;
545 }
546 fastboot_okay("");
547}
548
549
Ajay Dudani5c761132011-04-07 20:19:04 -0700550void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700551{
552 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700553 unsigned long long size = 0;
554
Greg Grisco6e754772011-06-23 12:19:39 -0700555 if (!strcmp(arg, "partition"))
556 {
557 dprintf(INFO, "Attempt to write partition image.\n");
558 if (mmc_write_partition(sz, (unsigned char *) data)) {
559 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700560 return;
561 }
562 }
Greg Grisco6e754772011-06-23 12:19:39 -0700563 else
564 {
565 ptn = mmc_ptn_offset((unsigned char *) arg);
566 if(ptn == 0) {
567 fastboot_fail("partition table doesn't exist");
568 return;
569 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700570
Greg Grisco6e754772011-06-23 12:19:39 -0700571 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
572 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
573 fastboot_fail("image is not a boot image");
574 return;
575 }
576 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700577
Greg Grisco6e754772011-06-23 12:19:39 -0700578 size = mmc_ptn_size((unsigned char *) arg);
579 if (ROUND_TO_PAGE(sz,511) > size) {
580 fastboot_fail("size too large");
581 return;
582 }
583 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
584 fastboot_fail("flash write failure");
585 return;
586 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700587 }
588 fastboot_okay("");
589 return;
590}
591
Greg Griscod6250552011-06-29 14:40:23 -0700592
593
Ajay Dudani5c761132011-04-07 20:19:04 -0700594void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
595{
596 unsigned int chunk;
597 unsigned int chunk_data_sz;
598 sparse_header_t *sparse_header;
599 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700600 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700601 unsigned long long ptn = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700602
Greg Griscod6250552011-06-29 14:40:23 -0700603 ptn = mmc_ptn_offset((unsigned char *) arg);
604 if (ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700605 fastboot_fail("partition table doesn't exist");
606 return;
607 }
608
609 /* Read and skip over sparse image header */
610 sparse_header = (sparse_header_t *) data;
611 data += sparse_header->file_hdr_sz;
612 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
613 {
614 /* Skip the remaining bytes in a header that is longer than
615 * we expected.
616 */
617 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
618 }
619
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700620 dprintf (SPEW, "=== Sparse Image Header ===\n");
621 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
622 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
623 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
624 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
625 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
626 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
627 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
628 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700629
630 /* Start processing chunks */
631 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
632 {
633 /* Read and skip over chunk header */
634 chunk_header = (chunk_header_t *) data;
635 data += sizeof(chunk_header_t);
636
637 dprintf (SPEW, "=== Chunk Header ===\n");
638 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
639 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
640 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
641
642 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
643 {
644 /* Skip the remaining bytes in a header that is longer than
645 * we expected.
646 */
647 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
648 }
649
650 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
651 switch (chunk_header->chunk_type)
652 {
653 case CHUNK_TYPE_RAW:
654 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
655 chunk_data_sz))
656 {
657 fastboot_fail("Bogus chunk size for chunk type Raw");
658 return;
659 }
660
Ajay Dudaniab18f022011-05-12 14:39:22 -0700661 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
662 chunk_data_sz,
663 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700664 {
665 fastboot_fail("flash write failure");
666 return;
667 }
668 total_blocks += chunk_header->chunk_sz;
669 data += chunk_data_sz;
670 break;
671
672 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700673 total_blocks += chunk_header->chunk_sz;
674 break;
675
Ajay Dudani5c761132011-04-07 20:19:04 -0700676 case CHUNK_TYPE_CRC:
677 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
678 {
679 fastboot_fail("Bogus chunk size for chunk type Dont Care");
680 return;
681 }
682 total_blocks += chunk_header->chunk_sz;
683 data += chunk_data_sz;
684 break;
685
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700686 default:
Ajay Dudani5c761132011-04-07 20:19:04 -0700687 fastboot_fail("Unknown chunk type");
688 return;
689 }
690 }
691
Ajay Dudani0c6927b2011-05-18 11:12:16 -0700692 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
693 total_blocks, sparse_header->total_blks);
694
695 if(total_blocks != sparse_header->total_blks)
696 {
697 fastboot_fail("sparse image write failure");
698 }
Ajay Dudani5c761132011-04-07 20:19:04 -0700699
700 fastboot_okay("");
701 return;
702}
703
704void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
705{
706 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700707 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
708 unsigned int *magic_number = (unsigned int *) data;
709 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700710
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700711 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
712 magic_number[1] == SSD_HEADER_MAGIC_1)
713 {
714#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -0700715 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700716#endif
Greg Griscod6250552011-06-29 14:40:23 -0700717 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700718 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
719 return;
720 }
721 }
722 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -0700723 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
724 cmd_flash_mmc_img(arg, data, sz);
725 else
726 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -0700727 return;
728}
729
Dima Zavin214cc642009-01-26 11:16:21 -0800730void cmd_flash(const char *arg, void *data, unsigned sz)
731{
732 struct ptentry *ptn;
733 struct ptable *ptable;
734 unsigned extra = 0;
735
736 ptable = flash_get_ptable();
737 if (ptable == NULL) {
738 fastboot_fail("partition table doesn't exist");
739 return;
740 }
741
742 ptn = ptable_find(ptable, arg);
743 if (ptn == NULL) {
744 fastboot_fail("unknown partition name");
745 return;
746 }
747
748 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
749 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
750 fastboot_fail("image is not a boot image");
751 return;
752 }
753 }
754
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800755 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +0530756 || !strcmp(ptn->name, "persist")) {
757 if (flash_ecc_bch_enabled())
758 /* Spare data bytes for 8 bit ECC increased by 4 */
759 extra = ((page_size >> 9) * 20);
760 else
761 extra = ((page_size >> 9) * 16);
762 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800763 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800764
765 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
766 if (flash_write(ptn, extra, data, sz)) {
767 fastboot_fail("flash write failure");
768 return;
769 }
770 dprintf(INFO, "partition '%s' updated\n", ptn->name);
771 fastboot_okay("");
772}
773
774void cmd_continue(const char *arg, void *data, unsigned sz)
775{
776 fastboot_okay("");
777 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700778 if (target_is_emmc_boot())
779 {
780 boot_linux_from_mmc();
781 }
782 else
783 {
784 boot_linux_from_flash();
785 }
Dima Zavin214cc642009-01-26 11:16:21 -0800786}
787
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800788void cmd_reboot(const char *arg, void *data, unsigned sz)
789{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700790 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800791 fastboot_okay("");
792 reboot_device(0);
793}
794
795void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
796{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700797 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800798 fastboot_okay("");
799 reboot_device(FASTBOOT_MODE);
800}
801
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700802void splash_screen ()
803{
804 struct ptentry *ptn;
805 struct ptable *ptable;
806 struct fbcon_config *fb_display = NULL;
807
808 if (!target_is_emmc_boot())
809 {
810 ptable = flash_get_ptable();
811 if (ptable == NULL) {
812 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -0700813 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700814 }
815
816 ptn = ptable_find(ptable, "splash");
817 if (ptn == NULL) {
818 dprintf(CRITICAL, "ERROR: No splash partition found\n");
819 } else {
820 fb_display = fbcon_display();
821 if (fb_display) {
822 if (flash_read(ptn, 0, fb_display->base,
823 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
824 fbcon_clear();
825 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
826 }
827 }
828 }
829 }
830}
831
Brian Swetland9c4c0752009-01-25 16:23:50 -0800832void aboot_init(const struct app_descriptor *app)
833{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800834 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700835 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700836 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700837
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700838 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700839 if (target_is_emmc_boot())
840 {
841 page_size = 2048;
842 page_mask = page_size - 1;
843 }
844 else
845 {
846 page_size = flash_page_size();
847 page_mask = page_size - 1;
848 }
849
Greg Griscod6250552011-06-29 14:40:23 -0700850 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700851 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800852 surf_udc_device.serialno = sn_buf;
853
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700854 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700855 if (keys_get_state(KEY_HOME) != 0)
856 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500857 if (keys_get_state(KEY_VOLUMEUP) != 0)
858 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700859 if(!boot_into_recovery)
860 {
861 if (keys_get_state(KEY_BACK) != 0)
862 goto fastboot;
863 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
864 goto fastboot;
865 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700866
867 #if NO_KEYPAD_DRIVER
868 /* With no keypad implementation, check the status of USB connection. */
869 /* If USB is connected then go into fastboot mode. */
870 usb_init = 1;
871 udc_init(&surf_udc_device);
872 if (usb_cable_status())
873 goto fastboot;
874 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700875
Ajay Dudani77421292010-10-27 19:34:06 -0700876 reboot_mode = check_reboot_mode();
877 if (reboot_mode == RECOVERY_MODE) {
878 boot_into_recovery = 1;
879 } else if(reboot_mode == FASTBOOT_MODE) {
880 goto fastboot;
881 }
882
Shashank Mittal23b8f422010-04-16 19:27:21 -0700883 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700884 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700885 if(emmc_recovery_init())
886 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700887 boot_linux_from_mmc();
888 }
889 else
890 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700891 recovery_init();
892 boot_linux_from_flash();
893 }
Dima Zavinb4283602009-01-26 16:36:57 -0800894 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
895 "to fastboot mode.\n");
896
897fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700898
Amol Jadi57abe4c2011-05-24 15:47:27 -0700899 target_fastboot_init();
900
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700901 if(!usb_init)
902 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800903
904 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700905
Shashank Mittal23b8f422010-04-16 19:27:21 -0700906 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700907 {
908 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700909 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700910 }
911 else
912 {
913 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700914 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700915 }
916
917 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800918 fastboot_register("reboot", cmd_reboot);
919 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700920 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800921 fastboot_publish("kernel", "lk");
Greg Grisco6e754772011-06-23 12:19:39 -0700922 mmc_dump_partition_info();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700923 sz = target_get_max_flash_size();
924 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800925 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800926}
927
928APP_START(aboot)
929 .init = aboot_init,
930APP_END
931