blob: 2bf9be4b3a1a3112fe923540379f1036a45a9505 [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>
Dima Zavin214cc642009-01-26 11:16:21 -080046
Shashank Mittal024c0332010-02-03 11:44:00 -080047#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080048#include "bootimg.h"
49#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070050#include "sparse_format.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080051
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070052#include "scm_decrypt.h"
53
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070054#define EXPAND(NAME) #NAME
55#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080056#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070057
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080058#ifdef MEMBASE
59#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
60#else
David Ng183a7422009-12-07 14:55:21 -080061#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080062#endif
63
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080064#define RECOVERY_MODE 0x77665502
65#define FASTBOOT_MODE 0x77665500
66
David Ng183a7422009-12-07 14:55:21 -080067static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080068static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070069static const char *battchg_pause = " androidboot.battchg_pause=true";
David Ng183a7422009-12-07 14:55:21 -080070
Ajay Dudani6cff85e2011-02-04 16:02:16 -080071static const char *baseband_apq = " androidboot.baseband=apq";
72static const char *baseband_msm = " androidboot.baseband=msm";
73static const char *baseband_csfb = " androidboot.baseband=csfb";
74static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080075
Brian Swetland9c4c0752009-01-25 16:23:50 -080076static struct udc_device surf_udc_device = {
77 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080078 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080079 .version_id = 0x0100,
80 .manufacturer = "Google",
81 .product = "Android",
82};
83
Dima Zavin42168f22009-01-30 11:52:22 -080084struct atag_ptbl_entry
85{
86 char name[16];
87 unsigned offset;
88 unsigned size;
89 unsigned flags;
90};
91
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080092char sn_buf[13];
Brian Swetland9c4c0752009-01-25 16:23:50 -080093void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080094unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080095unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080097void *target_get_scratch_address(void);
Vivek Mehta5f1c9d42011-04-01 20:11:59 -070098unsigned target_get_max_flash_size(void);
David Ng183a7422009-12-07 14:55:21 -080099int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800100void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800101void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700102unsigned int mmc_write (unsigned long long data_addr,
103 unsigned int data_len, unsigned int* in);
Shashank Mittal920798e2010-10-04 17:17:37 -0700104unsigned long long mmc_ptn_offset (unsigned char * name);
105unsigned long long mmc_ptn_size (unsigned char * name);
Shashank Mittalc648e712010-10-06 18:37:42 -0700106void display_shutdown(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800107
Dima Zavin42168f22009-01-30 11:52:22 -0800108static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
109{
110 struct atag_ptbl_entry atag_ptn;
111
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800112 if (ptn->type == TYPE_MODEM_PARTITION)
113 return;
Dima Zavin42168f22009-01-30 11:52:22 -0800114 memcpy(atag_ptn.name, ptn->name, 16);
115 atag_ptn.name[15] = '\0';
116 atag_ptn.offset = ptn->start;
117 atag_ptn.size = ptn->length;
118 atag_ptn.flags = ptn->flags;
119 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
120 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
121}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800122
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700123void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800124 const char *cmdline, unsigned machtype,
125 void *ramdisk, unsigned ramdisk_size)
126{
127 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800128 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800130 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800131 int cmdline_len = 0;
132 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700133 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800134
135 /* CORE */
136 *ptr++ = 2;
137 *ptr++ = 0x54410001;
138
139 if (ramdisk_size) {
140 *ptr++ = 4;
141 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800142 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800143 *ptr++ = ramdisk_size;
144 }
145
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800146 ptr = target_atag_mem(ptr);
147
David Ng183a7422009-12-07 14:55:21 -0800148 if (!target_is_emmc_boot()) {
149 /* Skip NAND partition ATAGS for eMMC boot */
150 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
151 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800152 for(i=0; i < ptable->count; i++) {
153 struct ptentry *ptn;
154 ptn = ptable_get(ptable, i);
155 if (ptn->type == TYPE_APPS_PARTITION)
156 pcount++;
157 }
158 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800159 sizeof(unsigned)));
160 *ptr++ = 0x4d534d70;
161 for (i = 0; i < ptable->count; ++i)
162 ptentry_to_tag(&ptr, ptable_get(ptable, i));
163 }
Dima Zavin42168f22009-01-30 11:52:22 -0800164 }
165
Brian Swetland9c4c0752009-01-25 16:23:50 -0800166 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800167 cmdline_len = strlen(cmdline);
168 have_cmdline = 1;
169 }
170 if (target_is_emmc_boot()) {
171 cmdline_len += strlen(emmc_cmdline);
172 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800173
174 cmdline_len += strlen(usb_sn_cmdline);
175 cmdline_len += strlen(sn_buf);
176
David Ngf773dde2010-07-26 19:55:08 -0700177 if (target_pause_for_battery_charge()) {
178 pause_at_bootup = 1;
179 cmdline_len += strlen(battchg_pause);
180 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800181
182 /* Determine correct androidboot.baseband to use */
183 switch(target_baseband())
184 {
185 case BASEBAND_APQ:
186 cmdline_len += strlen(baseband_apq);
187 break;
188
189 case BASEBAND_MSM:
190 cmdline_len += strlen(baseband_msm);
191 break;
192
193 case BASEBAND_CSFB:
194 cmdline_len += strlen(baseband_csfb);
195 break;
196
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800197 case BASEBAND_SVLTE2A:
198 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800199 break;
200 }
201
David Ng183a7422009-12-07 14:55:21 -0800202 if (cmdline_len > 0) {
203 const char *src;
204 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800205 unsigned n;
206 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800207 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800208 *ptr++ = (n / 4) + 2;
209 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800210 dst = (char *)ptr;
211 if (have_cmdline) {
212 src = cmdline;
213 while ((*dst++ = *src++));
214 }
215 if (target_is_emmc_boot()) {
216 src = emmc_cmdline;
217 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700218 have_cmdline = 1;
219 while ((*dst++ = *src++));
220 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800221
222 src = usb_sn_cmdline;
223 if (have_cmdline) --dst;
224 have_cmdline = 1;
225 while ((*dst++ = *src++));
226 src = sn_buf;
227 if (have_cmdline) --dst;
228 have_cmdline = 1;
229 while ((*dst++ = *src++));
230
David Ngf773dde2010-07-26 19:55:08 -0700231 if (pause_at_bootup) {
232 src = battchg_pause;
233 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800234 while ((*dst++ = *src++));
235 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800236
237 switch(target_baseband())
238 {
239 case BASEBAND_APQ:
240 src = baseband_apq;
241 if (have_cmdline) --dst;
242 while ((*dst++ = *src++));
243 break;
244
245 case BASEBAND_MSM:
246 src = baseband_msm;
247 if (have_cmdline) --dst;
248 while ((*dst++ = *src++));
249 break;
250
251 case BASEBAND_CSFB:
252 src = baseband_csfb;
253 if (have_cmdline) --dst;
254 while ((*dst++ = *src++));
255 break;
256
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800257 case BASEBAND_SVLTE2A:
258 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800259 if (have_cmdline) --dst;
260 while ((*dst++ = *src++));
261 break;
262 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800263 ptr += (n / 4);
264 }
265
266 /* END */
267 *ptr++ = 0;
268 *ptr++ = 0;
269
270 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
271 kernel, ramdisk, ramdisk_size);
272 if (cmdline)
273 dprintf(INFO, "cmdline: %s\n", cmdline);
274
275 enter_critical_section();
276 platform_uninit_timer();
277 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800278 arch_disable_mmu();
Shashank Mittalc648e712010-10-06 18:37:42 -0700279#if DISPLAY_SPLASH_SCREEN
280 display_shutdown();
281#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800282 entry(0, machtype, tags);
283
284}
285
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800286unsigned page_size = 0;
287unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800288
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800289#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800290
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800291static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800292
Shashank Mittal23b8f422010-04-16 19:27:21 -0700293int boot_linux_from_mmc(void)
294{
295 struct boot_img_hdr *hdr = (void*) buf;
296 struct boot_img_hdr *uhdr;
297 unsigned offset = 0;
298 unsigned long long ptn = 0;
299 unsigned n = 0;
300 const char *cmdline;
301
302 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
303 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
304 dprintf(INFO, "Unified boot method!\n");
305 hdr = uhdr;
306 goto unified_boot;
307 }
Shashank Mittal85b91f62010-10-30 10:12:38 -0700308 if(!boot_into_recovery)
309 {
310 ptn = mmc_ptn_offset("boot");
311 if(ptn == 0) {
312 dprintf(CRITICAL, "ERROR: No boot partition found\n");
313 return -1;
314 }
315 }
316 else
317 {
318 ptn = mmc_ptn_offset("recovery");
319 if(ptn == 0) {
320 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
321 return -1;
322 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700323 }
324
325 if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) {
326 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
327 return -1;
328 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700329
330 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700331 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700332 return -1;
333 }
334
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700335 if (hdr->page_size && (hdr->page_size != page_size)) {
336 page_size = hdr->page_size;
337 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700338 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700339 offset += page_size;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700340
341 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
342 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
343 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
344 return -1;
345 }
346 offset += n;
347
348 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800349 if(n != 0)
350 {
351 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
352 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
353 return -1;
354 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700355 }
356 offset += n;
357
358unified_boot:
359 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
360 hdr->kernel_size);
361 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
362 hdr->ramdisk_size);
363
364 if(hdr->cmdline[0]) {
365 cmdline = (char*) hdr->cmdline;
366 } else {
367 cmdline = DEFAULT_CMDLINE;
368 }
369 dprintf(INFO, "cmdline = '%s'\n", cmdline);
370
371 dprintf(INFO, "\nBooting Linux\n");
372 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
373 (const char *)cmdline, board_machtype(),
374 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
375
376 return 0;
377}
378
Dima Zavin214cc642009-01-26 11:16:21 -0800379int boot_linux_from_flash(void)
380{
381 struct boot_img_hdr *hdr = (void*) buf;
382 unsigned n;
383 struct ptentry *ptn;
384 struct ptable *ptable;
385 unsigned offset = 0;
386 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800387
David Ng183a7422009-12-07 14:55:21 -0800388 if (target_is_emmc_boot()) {
389 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
390 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
391 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
392 return -1;
393 }
394 goto continue_boot;
395 }
396
Dima Zavin214cc642009-01-26 11:16:21 -0800397 ptable = flash_get_ptable();
398 if (ptable == NULL) {
399 dprintf(CRITICAL, "ERROR: Partition table not found\n");
400 return -1;
401 }
402
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800403 if(!boot_into_recovery)
404 {
405 ptn = ptable_find(ptable, "boot");
406 if (ptn == NULL) {
407 dprintf(CRITICAL, "ERROR: No boot partition found\n");
408 return -1;
409 }
410 }
411 else
412 {
413 ptn = ptable_find(ptable, "recovery");
414 if (ptn == NULL) {
415 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
416 return -1;
417 }
Dima Zavin214cc642009-01-26 11:16:21 -0800418 }
419
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800420 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800421 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
422 return -1;
423 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800424 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800425
426 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700427 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800428 return -1;
429 }
430
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800431 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700432 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 -0800433 return -1;
434 }
435
436 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800437 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
438 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
439 return -1;
440 }
441 offset += n;
442
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800443 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800444 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
445 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
446 return -1;
447 }
448 offset += n;
449
David Ng183a7422009-12-07 14:55:21 -0800450continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800451 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
452 hdr->kernel_size);
453 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
454 hdr->ramdisk_size);
455
456 if(hdr->cmdline[0]) {
457 cmdline = (char*) hdr->cmdline;
458 } else {
459 cmdline = DEFAULT_CMDLINE;
460 }
461 dprintf(INFO, "cmdline = '%s'\n", cmdline);
462
463 /* TODO: create/pass atags to kernel */
464
465 dprintf(INFO, "\nBooting Linux\n");
466 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800467 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800468 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
469
470 return 0;
471}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800472
473void cmd_boot(const char *arg, void *data, unsigned sz)
474{
475 unsigned kernel_actual;
476 unsigned ramdisk_actual;
477 static struct boot_img_hdr hdr;
478 char *ptr = ((char*) data);
479
480 if (sz < sizeof(hdr)) {
481 fastboot_fail("invalid bootimage header");
482 return;
483 }
484
485 memcpy(&hdr, data, sizeof(hdr));
486
487 /* ensure commandline is terminated */
488 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
489
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700490 if(target_is_emmc_boot() && hdr.page_size) {
491 page_size = hdr.page_size;
492 page_mask = page_size - 1;
493 }
494
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800495 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
496 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800497
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700498 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800499 fastboot_fail("incomplete bootimage");
500 return;
501 }
502
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700503 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
504 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505
506 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800507 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800508 udc_stop();
509
Brian Swetland9c4c0752009-01-25 16:23:50 -0800510 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800511 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800512 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
513}
514
Dima Zavin214cc642009-01-26 11:16:21 -0800515void cmd_erase(const char *arg, void *data, unsigned sz)
516{
517 struct ptentry *ptn;
518 struct ptable *ptable;
519
520 ptable = flash_get_ptable();
521 if (ptable == NULL) {
522 fastboot_fail("partition table doesn't exist");
523 return;
524 }
525
526 ptn = ptable_find(ptable, arg);
527 if (ptn == NULL) {
528 fastboot_fail("unknown partition name");
529 return;
530 }
531
532 if (flash_erase(ptn)) {
533 fastboot_fail("failed to erase partition");
534 return;
535 }
536 fastboot_okay("");
537}
538
Bikas Gurungd48bd242010-09-04 19:54:32 -0700539
540void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
541{
542 unsigned long long ptn = 0;
543 unsigned int out[512] = {0};
544
545 ptn = mmc_ptn_offset(arg);
546 if(ptn == 0) {
547 fastboot_fail("partition table doesn't exist");
548 return;
549 }
550
551
552 /* Simple inefficient version of erase. Just writing
553 0 in first block */
554 if (mmc_write(ptn , 512, (unsigned int *)out)) {
555 fastboot_fail("failed to erase partition");
556 return;
557 }
558 fastboot_okay("");
559}
560
561
Ajay Dudani5c761132011-04-07 20:19:04 -0700562void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700563{
564 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700565 unsigned long long size = 0;
566
Shashank Mittal23b8f422010-04-16 19:27:21 -0700567 ptn = mmc_ptn_offset(arg);
568 if(ptn == 0) {
569 fastboot_fail("partition table doesn't exist");
570 return;
571 }
572
573 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
574 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
575 fastboot_fail("image is not a boot image");
576 return;
577 }
578 }
579
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700580 size = mmc_ptn_size(arg);
581 if (ROUND_TO_PAGE(sz,511) > size) {
582 fastboot_fail("size too large");
583 return;
584 }
585
Shashank Mittal23b8f422010-04-16 19:27:21 -0700586 if (mmc_write(ptn , sz, (unsigned int *)data)) {
587 fastboot_fail("flash write failure");
588 return;
589 }
590 fastboot_okay("");
591 return;
592}
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;
600 uint32_t crc32 = 0;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700601 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700602 unsigned long long ptn = 0;
603 unsigned long long size = 0;
604
605 ptn = mmc_ptn_offset(arg);
606 if(ptn == 0) {
607 fastboot_fail("partition table doesn't exist");
608 return;
609 }
610
611 /* Read and skip over sparse image header */
612 sparse_header = (sparse_header_t *) data;
613 data += sparse_header->file_hdr_sz;
614 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
615 {
616 /* Skip the remaining bytes in a header that is longer than
617 * we expected.
618 */
619 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
620 }
621
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700622 dprintf (SPEW, "=== Sparse Image Header ===\n");
623 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
624 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
625 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
626 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
627 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
628 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
629 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
630 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700631
632 /* Start processing chunks */
633 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
634 {
635 /* Read and skip over chunk header */
636 chunk_header = (chunk_header_t *) data;
637 data += sizeof(chunk_header_t);
638
639 dprintf (SPEW, "=== Chunk Header ===\n");
640 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
641 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
642 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
643
644 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
645 {
646 /* Skip the remaining bytes in a header that is longer than
647 * we expected.
648 */
649 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
650 }
651
652 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
653 switch (chunk_header->chunk_type)
654 {
655 case CHUNK_TYPE_RAW:
656 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
657 chunk_data_sz))
658 {
659 fastboot_fail("Bogus chunk size for chunk type Raw");
660 return;
661 }
662
Ajay Dudaniab18f022011-05-12 14:39:22 -0700663 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
664 chunk_data_sz,
665 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700666 {
667 fastboot_fail("flash write failure");
668 return;
669 }
670 total_blocks += chunk_header->chunk_sz;
671 data += chunk_data_sz;
672 break;
673
674 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700675 total_blocks += chunk_header->chunk_sz;
676 break;
677
Ajay Dudani5c761132011-04-07 20:19:04 -0700678 case CHUNK_TYPE_CRC:
679 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
680 {
681 fastboot_fail("Bogus chunk size for chunk type Dont Care");
682 return;
683 }
684 total_blocks += chunk_header->chunk_sz;
685 data += chunk_data_sz;
686 break;
687
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700688 default:
Ajay Dudani5c761132011-04-07 20:19:04 -0700689 fastboot_fail("Unknown chunk type");
690 return;
691 }
692 }
693
Ajay Dudani0c6927b2011-05-18 11:12:16 -0700694 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
695 total_blocks, sparse_header->total_blks);
696
697 if(total_blocks != sparse_header->total_blks)
698 {
699 fastboot_fail("sparse image write failure");
700 }
Ajay Dudani5c761132011-04-07 20:19:04 -0700701
702 fastboot_okay("");
703 return;
704}
705
706void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
707{
708 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700709 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
710 unsigned int *magic_number = (unsigned int *) data;
711 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700712
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700713 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
714 magic_number[1] == SSD_HEADER_MAGIC_1)
715 {
716#ifdef SSD_ENABLE
717 ret = decrypt_img_scm(&data, &sz);
718#endif
719 if(ret != 0)
720 {
721 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
722 return;
723 }
724 }
725 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -0700726 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
727 cmd_flash_mmc_img(arg, data, sz);
728 else
729 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -0700730 return;
731}
732
Dima Zavin214cc642009-01-26 11:16:21 -0800733void cmd_flash(const char *arg, void *data, unsigned sz)
734{
735 struct ptentry *ptn;
736 struct ptable *ptable;
737 unsigned extra = 0;
738
739 ptable = flash_get_ptable();
740 if (ptable == NULL) {
741 fastboot_fail("partition table doesn't exist");
742 return;
743 }
744
745 ptn = ptable_find(ptable, arg);
746 if (ptn == NULL) {
747 fastboot_fail("unknown partition name");
748 return;
749 }
750
751 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
752 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
753 fastboot_fail("image is not a boot image");
754 return;
755 }
756 }
757
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800758 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +0530759 || !strcmp(ptn->name, "persist")) {
760 if (flash_ecc_bch_enabled())
761 /* Spare data bytes for 8 bit ECC increased by 4 */
762 extra = ((page_size >> 9) * 20);
763 else
764 extra = ((page_size >> 9) * 16);
765 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800766 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800767
768 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
769 if (flash_write(ptn, extra, data, sz)) {
770 fastboot_fail("flash write failure");
771 return;
772 }
773 dprintf(INFO, "partition '%s' updated\n", ptn->name);
774 fastboot_okay("");
775}
776
777void cmd_continue(const char *arg, void *data, unsigned sz)
778{
779 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800780 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800781 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700782 if (target_is_emmc_boot())
783 {
784 boot_linux_from_mmc();
785 }
786 else
787 {
788 boot_linux_from_flash();
789 }
Dima Zavin214cc642009-01-26 11:16:21 -0800790}
791
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800792void cmd_reboot(const char *arg, void *data, unsigned sz)
793{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700794 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800795 fastboot_okay("");
796 reboot_device(0);
797}
798
799void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
800{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700801 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800802 fastboot_okay("");
803 reboot_device(FASTBOOT_MODE);
804}
805
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700806void splash_screen ()
807{
808 struct ptentry *ptn;
809 struct ptable *ptable;
810 struct fbcon_config *fb_display = NULL;
811
812 if (!target_is_emmc_boot())
813 {
814 ptable = flash_get_ptable();
815 if (ptable == NULL) {
816 dprintf(CRITICAL, "ERROR: Partition table not found\n");
817 return -1;
818 }
819
820 ptn = ptable_find(ptable, "splash");
821 if (ptn == NULL) {
822 dprintf(CRITICAL, "ERROR: No splash partition found\n");
823 } else {
824 fb_display = fbcon_display();
825 if (fb_display) {
826 if (flash_read(ptn, 0, fb_display->base,
827 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
828 fbcon_clear();
829 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
830 }
831 }
832 }
833 }
834}
835
Brian Swetland9c4c0752009-01-25 16:23:50 -0800836void aboot_init(const struct app_descriptor *app)
837{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800838 unsigned reboot_mode = 0;
839 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700840 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700841 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700842
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700843 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700844 if (target_is_emmc_boot())
845 {
846 page_size = 2048;
847 page_mask = page_size - 1;
848 }
849 else
850 {
851 page_size = flash_page_size();
852 page_mask = page_size - 1;
853 }
854
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700855 /* Display splash screen if enabled */
856 #if DISPLAY_SPLASH_SCREEN
857 display_init();
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700858 dprintf(SPEW, "Diplay initialized\n");
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700859 disp_init = 1;
Chandan Uddaraju40b227d2010-08-03 19:25:41 -0700860 diplay_image_on_screen();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700861 #endif
862
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800863 target_serialno(sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700864 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800865 surf_udc_device.serialno = sn_buf;
866
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700867 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700868 if (keys_get_state(KEY_HOME) != 0)
869 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500870 if (keys_get_state(KEY_VOLUMEUP) != 0)
871 boot_into_recovery = 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700872 if (keys_get_state(KEY_BACK) != 0)
873 goto fastboot;
Wentao Xu153902c2010-12-20 16:20:52 -0500874 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700875 goto fastboot;
876
877 #if NO_KEYPAD_DRIVER
878 /* With no keypad implementation, check the status of USB connection. */
879 /* If USB is connected then go into fastboot mode. */
880 usb_init = 1;
881 udc_init(&surf_udc_device);
882 if (usb_cable_status())
883 goto fastboot;
884 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700885
Ajay Dudani77421292010-10-27 19:34:06 -0700886 reboot_mode = check_reboot_mode();
887 if (reboot_mode == RECOVERY_MODE) {
888 boot_into_recovery = 1;
889 } else if(reboot_mode == FASTBOOT_MODE) {
890 goto fastboot;
891 }
892
Shashank Mittal23b8f422010-04-16 19:27:21 -0700893 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700894 {
895 boot_linux_from_mmc();
896 }
897 else
898 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700899 recovery_init();
900 boot_linux_from_flash();
901 }
Dima Zavinb4283602009-01-26 16:36:57 -0800902 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
903 "to fastboot mode.\n");
904
905fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700906
Amol Jadi57abe4c2011-05-24 15:47:27 -0700907 target_fastboot_init();
908
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700909 if(!usb_init)
910 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800911
912 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700913
Shashank Mittal23b8f422010-04-16 19:27:21 -0700914 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700915 {
916 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700917 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700918 }
919 else
920 {
921 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700922 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700923 }
924
925 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800926 fastboot_register("reboot", cmd_reboot);
927 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700928 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800929 fastboot_publish("kernel", "lk");
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700930 sz = target_get_max_flash_size();
931 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800932 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700933 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800934}
935
936APP_START(aboot)
937 .init = aboot_init,
938APP_END
939