blob: 75f51ae3c86a26885e6b0aebcdf4a2160bf81a76 [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>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Dima Zavin214cc642009-01-26 11:16:21 -080051
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080053#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080054#include "bootimg.h"
55#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070056#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070057#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070058#include "devinfo.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080059
Shashank Mittal162244e2011-08-08 19:01:25 -070060#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070061
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070062#define EXPAND(NAME) #NAME
63#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080064#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070065
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080066#ifdef MEMBASE
67#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
68#else
David Ng183a7422009-12-07 14:55:21 -080069#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080070#endif
71
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080072#define RECOVERY_MODE 0x77665502
73#define FASTBOOT_MODE 0x77665500
74
David Ng183a7422009-12-07 14:55:21 -080075static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080076static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070077static const char *battchg_pause = " androidboot.battchg_pause=true";
Shashank Mittalcd98d472011-08-02 14:29:24 -070078static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080079
Ajay Dudani6cff85e2011-02-04 16:02:16 -080080static const char *baseband_apq = " androidboot.baseband=apq";
81static const char *baseband_msm = " androidboot.baseband=msm";
82static const char *baseband_csfb = " androidboot.baseband=csfb";
83static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -070084static const char *baseband_mdm = " androidboot.baseband=mdm";
Ajay Dudanid04110c2011-01-17 23:55:07 -080085
Shashank Mittalcd98d472011-08-02 14:29:24 -070086/* Assuming unauthorized kernel image by default */
87static int auth_kernel_img = 0;
88
Shashank Mittal162244e2011-08-08 19:01:25 -070089static device_info device = {DEVICE_MAGIC, 0, 0};
90
Brian Swetland9c4c0752009-01-25 16:23:50 -080091static struct udc_device surf_udc_device = {
92 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080093 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080094 .version_id = 0x0100,
95 .manufacturer = "Google",
96 .product = "Android",
97};
98
Dima Zavin42168f22009-01-30 11:52:22 -080099struct atag_ptbl_entry
100{
101 char name[16];
102 unsigned offset;
103 unsigned size;
104 unsigned flags;
105};
106
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800107char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700108
Greg Griscod2471ef2011-07-14 13:00:42 -0700109extern int emmc_recovery_init(void);
110
Kinson Chik0b1c8162011-08-31 16:31:57 -0700111#if NO_KEYPAD_DRIVER
112extern int fastboot_trigger(void);
113#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700114
Dima Zavin42168f22009-01-30 11:52:22 -0800115static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
116{
117 struct atag_ptbl_entry atag_ptn;
118
Greg Griscod6250552011-06-29 14:40:23 -0700119 if (ptn->type == TYPE_MODEM_PARTITION) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800120 return;
Greg Griscod6250552011-06-29 14:40:23 -0700121 }
Dima Zavin42168f22009-01-30 11:52:22 -0800122 memcpy(atag_ptn.name, ptn->name, 16);
123 atag_ptn.name[15] = '\0';
124 atag_ptn.offset = ptn->start;
125 atag_ptn.size = ptn->length;
126 atag_ptn.flags = ptn->flags;
127 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
128 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
129}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800130
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700131void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800132 const char *cmdline, unsigned machtype,
133 void *ramdisk, unsigned ramdisk_size)
134{
135 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800136 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800137 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800138 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800139 int cmdline_len = 0;
140 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700141 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800142
143 /* CORE */
144 *ptr++ = 2;
145 *ptr++ = 0x54410001;
146
147 if (ramdisk_size) {
148 *ptr++ = 4;
149 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800150 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800151 *ptr++ = ramdisk_size;
152 }
153
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800154 ptr = target_atag_mem(ptr);
155
David Ng183a7422009-12-07 14:55:21 -0800156 if (!target_is_emmc_boot()) {
157 /* Skip NAND partition ATAGS for eMMC boot */
158 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
159 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800160 for(i=0; i < ptable->count; i++) {
161 struct ptentry *ptn;
162 ptn = ptable_get(ptable, i);
163 if (ptn->type == TYPE_APPS_PARTITION)
164 pcount++;
165 }
166 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800167 sizeof(unsigned)));
168 *ptr++ = 0x4d534d70;
169 for (i = 0; i < ptable->count; ++i)
170 ptentry_to_tag(&ptr, ptable_get(ptable, i));
171 }
Dima Zavin42168f22009-01-30 11:52:22 -0800172 }
173
Brian Swetland9c4c0752009-01-25 16:23:50 -0800174 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800175 cmdline_len = strlen(cmdline);
176 have_cmdline = 1;
177 }
178 if (target_is_emmc_boot()) {
179 cmdline_len += strlen(emmc_cmdline);
180 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800181
182 cmdline_len += strlen(usb_sn_cmdline);
183 cmdline_len += strlen(sn_buf);
184
David Ngf773dde2010-07-26 19:55:08 -0700185 if (target_pause_for_battery_charge()) {
186 pause_at_bootup = 1;
187 cmdline_len += strlen(battchg_pause);
188 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800189
Shashank Mittalcd98d472011-08-02 14:29:24 -0700190 if(target_use_signed_kernel() && auth_kernel_img) {
191 cmdline_len += strlen(auth_kernel);
192 }
193
Ajay Dudanid04110c2011-01-17 23:55:07 -0800194 /* Determine correct androidboot.baseband to use */
195 switch(target_baseband())
196 {
197 case BASEBAND_APQ:
198 cmdline_len += strlen(baseband_apq);
199 break;
200
201 case BASEBAND_MSM:
202 cmdline_len += strlen(baseband_msm);
203 break;
204
205 case BASEBAND_CSFB:
206 cmdline_len += strlen(baseband_csfb);
207 break;
208
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800209 case BASEBAND_SVLTE2A:
210 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800211 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700212
213 case BASEBAND_MDM:
214 cmdline_len += strlen(baseband_mdm);
215 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800216 }
217
David Ng183a7422009-12-07 14:55:21 -0800218 if (cmdline_len > 0) {
219 const char *src;
220 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800221 unsigned n;
222 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800223 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800224 *ptr++ = (n / 4) + 2;
225 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800226 dst = (char *)ptr;
227 if (have_cmdline) {
228 src = cmdline;
229 while ((*dst++ = *src++));
230 }
231 if (target_is_emmc_boot()) {
232 src = emmc_cmdline;
233 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700234 have_cmdline = 1;
235 while ((*dst++ = *src++));
236 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800237
238 src = usb_sn_cmdline;
239 if (have_cmdline) --dst;
240 have_cmdline = 1;
241 while ((*dst++ = *src++));
242 src = sn_buf;
243 if (have_cmdline) --dst;
244 have_cmdline = 1;
245 while ((*dst++ = *src++));
246
David Ngf773dde2010-07-26 19:55:08 -0700247 if (pause_at_bootup) {
248 src = battchg_pause;
249 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800250 while ((*dst++ = *src++));
251 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800252
Shashank Mittalcd98d472011-08-02 14:29:24 -0700253 if(target_use_signed_kernel() && auth_kernel_img) {
254 src = auth_kernel;
255 if (have_cmdline) --dst;
256 while ((*dst++ = *src++));
257 }
258
Ajay Dudanid04110c2011-01-17 23:55:07 -0800259 switch(target_baseband())
260 {
261 case BASEBAND_APQ:
262 src = baseband_apq;
263 if (have_cmdline) --dst;
264 while ((*dst++ = *src++));
265 break;
266
267 case BASEBAND_MSM:
268 src = baseband_msm;
269 if (have_cmdline) --dst;
270 while ((*dst++ = *src++));
271 break;
272
273 case BASEBAND_CSFB:
274 src = baseband_csfb;
275 if (have_cmdline) --dst;
276 while ((*dst++ = *src++));
277 break;
278
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800279 case BASEBAND_SVLTE2A:
280 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800281 if (have_cmdline) --dst;
282 while ((*dst++ = *src++));
283 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700284
285 case BASEBAND_MDM:
286 src = baseband_mdm;
287 if (have_cmdline) --dst;
288 while ((*dst++ = *src++));
289 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800290 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800291 ptr += (n / 4);
292 }
293
294 /* END */
295 *ptr++ = 0;
296 *ptr++ = 0;
297
298 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
299 kernel, ramdisk, ramdisk_size);
300 if (cmdline)
301 dprintf(INFO, "cmdline: %s\n", cmdline);
302
303 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700304 /* do any platform specific cleanup before kernel entry */
305 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800306 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800307 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800308 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800309}
310
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800311unsigned page_size = 0;
312unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800313
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800314#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800315
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800316static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800317
Shashank Mittal23b8f422010-04-16 19:27:21 -0700318int boot_linux_from_mmc(void)
319{
320 struct boot_img_hdr *hdr = (void*) buf;
321 struct boot_img_hdr *uhdr;
322 unsigned offset = 0;
323 unsigned long long ptn = 0;
324 unsigned n = 0;
325 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700326 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700327
Shashank Mittalcd98d472011-08-02 14:29:24 -0700328 unsigned char *image_addr = 0;
329 unsigned kernel_actual;
330 unsigned ramdisk_actual;
331 unsigned imagesize_actual;
332
Shashank Mittal23b8f422010-04-16 19:27:21 -0700333 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
334 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
335 dprintf(INFO, "Unified boot method!\n");
336 hdr = uhdr;
337 goto unified_boot;
338 }
Greg Griscod6250552011-06-29 14:40:23 -0700339 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700340 index = partition_get_index("boot");
341 ptn = partition_get_offset(index);
342 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700343 dprintf(CRITICAL, "ERROR: No boot partition found\n");
344 return -1;
345 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700346 }
347 else {
348 index = partition_get_index("recovery");
349 ptn = partition_get_offset(index);
350 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700351 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
352 return -1;
353 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700354 }
355
Greg Griscod6250552011-06-29 14:40:23 -0700356 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700357 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
358 return -1;
359 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700360
361 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700362 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700363 return -1;
364 }
365
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700366 if (hdr->page_size && (hdr->page_size != page_size)) {
367 page_size = hdr->page_size;
368 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700369 }
370
Shashank Mittalcd98d472011-08-02 14:29:24 -0700371 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700372 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800373 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700374 image_addr = (unsigned char *)target_get_scratch_address();
375 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
376 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
377 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
378
379 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700380
381 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700382 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700383
Shashank Mittalcd98d472011-08-02 14:29:24 -0700384 /* Read image without signature */
385 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
386 {
387 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
388 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800389 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700390
391 offset = imagesize_actual;
392 /* Read signature */
393 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
394 {
395 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
396 }
397 else
398 {
399 auth_kernel_img = image_verify((unsigned char *)image_addr,
400 (unsigned char *)(image_addr + imagesize_actual),
401 imagesize_actual,
402 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700403
404 if(auth_kernel_img)
405 {
406 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700407 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700408 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700409 }
410
411 /* Move kernel and ramdisk to correct address */
412 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
413 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700414
415 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700416 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700417 {
418 write_device_info_mmc(&device);
419 #ifdef TZ_TAMPER_FUSE
420 set_tamper_fuse_cmd();
421 #endif
422 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700423 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700424 else
425 {
426 offset += page_size;
427
428 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
429 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
430 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
431 return -1;
432 }
433 offset += n;
434
435 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
436 if(n != 0)
437 {
438 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
439 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
440 return -1;
441 }
442 }
443 offset += n;
444 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700445
446unified_boot:
447 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
448 hdr->kernel_size);
449 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
450 hdr->ramdisk_size);
451
452 if(hdr->cmdline[0]) {
453 cmdline = (char*) hdr->cmdline;
454 } else {
455 cmdline = DEFAULT_CMDLINE;
456 }
457 dprintf(INFO, "cmdline = '%s'\n", cmdline);
458
459 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700460 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700461 (const char *)cmdline, board_machtype(),
462 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
463
464 return 0;
465}
466
Dima Zavin214cc642009-01-26 11:16:21 -0800467int boot_linux_from_flash(void)
468{
469 struct boot_img_hdr *hdr = (void*) buf;
470 unsigned n;
471 struct ptentry *ptn;
472 struct ptable *ptable;
473 unsigned offset = 0;
474 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800475
Shashank Mittalcd98d472011-08-02 14:29:24 -0700476 unsigned char *image_addr = 0;
477 unsigned kernel_actual;
478 unsigned ramdisk_actual;
479 unsigned imagesize_actual;
480
David Ng183a7422009-12-07 14:55:21 -0800481 if (target_is_emmc_boot()) {
482 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
483 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
484 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
485 return -1;
486 }
487 goto continue_boot;
488 }
489
Dima Zavin214cc642009-01-26 11:16:21 -0800490 ptable = flash_get_ptable();
491 if (ptable == NULL) {
492 dprintf(CRITICAL, "ERROR: Partition table not found\n");
493 return -1;
494 }
495
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800496 if(!boot_into_recovery)
497 {
498 ptn = ptable_find(ptable, "boot");
499 if (ptn == NULL) {
500 dprintf(CRITICAL, "ERROR: No boot partition found\n");
501 return -1;
502 }
503 }
504 else
505 {
506 ptn = ptable_find(ptable, "recovery");
507 if (ptn == NULL) {
508 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
509 return -1;
510 }
Dima Zavin214cc642009-01-26 11:16:21 -0800511 }
512
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800513 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800514 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
515 return -1;
516 }
Dima Zavin214cc642009-01-26 11:16:21 -0800517
518 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700519 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800520 return -1;
521 }
522
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800523 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700524 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 -0800525 return -1;
526 }
527
Shashank Mittalcd98d472011-08-02 14:29:24 -0700528 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700529 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700530 {
531 image_addr = (unsigned char *)target_get_scratch_address();
532 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
533 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
534 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800535
Shashank Mittalcd98d472011-08-02 14:29:24 -0700536 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700537
538 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700539 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700540
Shashank Mittalcd98d472011-08-02 14:29:24 -0700541 /* Read image without signature */
542 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
543 {
544 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
545 return -1;
546 }
Dima Zavin214cc642009-01-26 11:16:21 -0800547
Shashank Mittalcd98d472011-08-02 14:29:24 -0700548 offset = imagesize_actual;
549 /* Read signature */
550 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
551 {
552 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
553 }
554 else
555 {
556
557 /* Verify signature */
558 auth_kernel_img = image_verify((unsigned char *)image_addr,
559 (unsigned char *)(image_addr + imagesize_actual),
560 imagesize_actual,
561 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700562
563 if(auth_kernel_img)
564 {
565 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700566 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700567 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700568 }
569
570 /* Move kernel and ramdisk to correct address */
571 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
572 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700573
574 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700575 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700576 {
577 write_device_info_flash(&device);
578 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700579 }
580 else
581 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700582 offset = page_size;
583
Shashank Mittalcd98d472011-08-02 14:29:24 -0700584 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
585 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
586 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
587 return -1;
588 }
589 offset += n;
590
591 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
592 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
593 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
594 return -1;
595 }
596 offset += n;
597 }
David Ng183a7422009-12-07 14:55:21 -0800598continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800599 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
600 hdr->kernel_size);
601 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
602 hdr->ramdisk_size);
603
604 if(hdr->cmdline[0]) {
605 cmdline = (char*) hdr->cmdline;
606 } else {
607 cmdline = DEFAULT_CMDLINE;
608 }
609 dprintf(INFO, "cmdline = '%s'\n", cmdline);
610
611 /* TODO: create/pass atags to kernel */
612
613 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700614 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800615 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800616 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
617
618 return 0;
619}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800620
Shashank Mittal162244e2011-08-08 19:01:25 -0700621unsigned char info_buf[4096];
622void write_device_info_mmc(device_info *dev)
623{
624 struct device_info *info = (void*) info_buf;
625 unsigned long long ptn = 0;
626 unsigned long long size;
627 int index = INVALID_PTN;
628
629 index = partition_get_index("aboot");
630 ptn = partition_get_offset(index);
631 if(ptn == 0)
632 {
633 return;
634 }
635
636 size = partition_get_size(index);
637
638 memcpy(info, dev, sizeof(device_info));
639
640 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
641 {
642 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
643 return;
644 }
645}
646
647void read_device_info_mmc(device_info *dev)
648{
649 struct device_info *info = (void*) info_buf;
650 unsigned long long ptn = 0;
651 unsigned long long size;
652 int index = INVALID_PTN;
653
654 index = partition_get_index("aboot");
655 ptn = partition_get_offset(index);
656 if(ptn == 0)
657 {
658 return;
659 }
660
661 size = partition_get_size(index);
662
663 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
664 {
665 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
666 return;
667 }
668
669 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
670 {
671 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
672 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700673 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700674
675 write_device_info_mmc(info);
676 }
677 memcpy(dev, info, sizeof(device_info));
678}
679
680void write_device_info_flash(device_info *dev)
681{
682 struct device_info *info = (void *) info_buf;
683 struct ptentry *ptn;
684 struct ptable *ptable;
685
686 ptable = flash_get_ptable();
687 if (ptable == NULL)
688 {
689 dprintf(CRITICAL, "ERROR: Partition table not found\n");
690 return;
691 }
692
693 ptn = ptable_find(ptable, "devinfo");
694 if (ptn == NULL)
695 {
696 dprintf(CRITICAL, "ERROR: No boot partition found\n");
697 return;
698 }
699
700 memcpy(info, dev, sizeof(device_info));
701
702 if (flash_write(ptn, 0, (void *)info_buf, page_size))
703 {
704 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
705 return;
706 }
707}
708
709void read_device_info_flash(device_info *dev)
710{
711 struct device_info *info = (void*) info_buf;
712 struct ptentry *ptn;
713 struct ptable *ptable;
714
715 ptable = flash_get_ptable();
716 if (ptable == NULL)
717 {
718 dprintf(CRITICAL, "ERROR: Partition table not found\n");
719 return;
720 }
721
722 ptn = ptable_find(ptable, "devinfo");
723 if (ptn == NULL)
724 {
725 dprintf(CRITICAL, "ERROR: No boot partition found\n");
726 return;
727 }
728
729 if (flash_read(ptn, 0, (void *)info_buf, page_size))
730 {
731 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
732 return;
733 }
734
735 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
736 {
737 while(1);
738 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
739 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700740 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700741 write_device_info_flash(info);
742 }
743 memcpy(dev, info, sizeof(device_info));
744}
745
746void write_device_info(device_info *dev)
747{
748 if(target_is_emmc_boot())
749 {
750 write_device_info_mmc(dev);
751 }
752 else
753 {
754 write_device_info_flash(dev);
755 }
756}
757
758void read_device_info(device_info *dev)
759{
760 if(target_is_emmc_boot())
761 {
762 read_device_info_mmc(dev);
763 }
764 else
765 {
766 read_device_info_flash(dev);
767 }
768}
769
770void reset_device_info()
771{
772 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700773 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700774 write_device_info(&device);
775}
776
777void set_device_root()
778{
779 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700780 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700781 write_device_info(&device);
782}
783
Brian Swetland9c4c0752009-01-25 16:23:50 -0800784void cmd_boot(const char *arg, void *data, unsigned sz)
785{
786 unsigned kernel_actual;
787 unsigned ramdisk_actual;
788 static struct boot_img_hdr hdr;
789 char *ptr = ((char*) data);
790
791 if (sz < sizeof(hdr)) {
792 fastboot_fail("invalid bootimage header");
793 return;
794 }
795
796 memcpy(&hdr, data, sizeof(hdr));
797
798 /* ensure commandline is terminated */
799 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
800
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700801 if(target_is_emmc_boot() && hdr.page_size) {
802 page_size = hdr.page_size;
803 page_mask = page_size - 1;
804 }
805
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800806 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
807 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800808
Shashank Mittal1f0e2662011-09-01 15:06:00 -0700809 /* sz should have atleast raw boot image */
810 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800811 fastboot_fail("incomplete bootimage");
812 return;
813 }
814
Ajay Dudanie28a6072011-07-01 13:59:46 -0700815 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
816 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800817
818 fastboot_okay("");
819 udc_stop();
820
Amol Jadie67872e2011-06-27 14:14:11 -0700821 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800822 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700823 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800824}
825
Dima Zavin214cc642009-01-26 11:16:21 -0800826void cmd_erase(const char *arg, void *data, unsigned sz)
827{
828 struct ptentry *ptn;
829 struct ptable *ptable;
830
831 ptable = flash_get_ptable();
832 if (ptable == NULL) {
833 fastboot_fail("partition table doesn't exist");
834 return;
835 }
836
837 ptn = ptable_find(ptable, arg);
838 if (ptn == NULL) {
839 fastboot_fail("unknown partition name");
840 return;
841 }
842
843 if (flash_erase(ptn)) {
844 fastboot_fail("failed to erase partition");
845 return;
846 }
847 fastboot_okay("");
848}
849
Bikas Gurungd48bd242010-09-04 19:54:32 -0700850
851void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
852{
853 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700854 int index = INVALID_PTN;
Neeti Desaica8c9602011-10-06 11:40:00 -0700855 unsigned long long size;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700856
Kinson Chikf1a43512011-07-14 11:28:39 -0700857 index = partition_get_index(arg);
858 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -0700859 size = partition_get_size(index);
860
Kinson Chikf1a43512011-07-14 11:28:39 -0700861 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -0700862 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700863 return;
864 }
Neeti Desaica8c9602011-10-06 11:40:00 -0700865 if (mmc_erase_card(ptn, size)) {
866 fastboot_fail("Failed to erase partition\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700867 return;
868 }
869 fastboot_okay("");
870}
871
872
Ajay Dudani5c761132011-04-07 20:19:04 -0700873void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700874{
875 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700876 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700877 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700878
Greg Grisco6e754772011-06-23 12:19:39 -0700879 if (!strcmp(arg, "partition"))
880 {
881 dprintf(INFO, "Attempt to write partition image.\n");
882 if (mmc_write_partition(sz, (unsigned char *) data)) {
883 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700884 return;
885 }
886 }
Greg Grisco6e754772011-06-23 12:19:39 -0700887 else
888 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700889 index = partition_get_index(arg);
890 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700891 if(ptn == 0) {
892 fastboot_fail("partition table doesn't exist");
893 return;
894 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700895
Greg Grisco6e754772011-06-23 12:19:39 -0700896 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
897 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
898 fastboot_fail("image is not a boot image");
899 return;
900 }
901 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700902
Kinson Chikf1a43512011-07-14 11:28:39 -0700903 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700904 if (ROUND_TO_PAGE(sz,511) > size) {
905 fastboot_fail("size too large");
906 return;
907 }
908 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
909 fastboot_fail("flash write failure");
910 return;
911 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700912 }
913 fastboot_okay("");
914 return;
915}
916
Ajay Dudani5c761132011-04-07 20:19:04 -0700917void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
918{
919 unsigned int chunk;
920 unsigned int chunk_data_sz;
921 sparse_header_t *sparse_header;
922 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700923 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700924 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530925 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700926 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700927
Kinson Chikf1a43512011-07-14 11:28:39 -0700928 index = partition_get_index(arg);
929 ptn = partition_get_offset(index);
930 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700931 fastboot_fail("partition table doesn't exist");
932 return;
933 }
934
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530935 size = partition_get_size(index);
936 if (ROUND_TO_PAGE(sz,511) > size) {
937 fastboot_fail("size too large");
938 return;
939 }
940
Ajay Dudani5c761132011-04-07 20:19:04 -0700941 /* Read and skip over sparse image header */
942 sparse_header = (sparse_header_t *) data;
943 data += sparse_header->file_hdr_sz;
944 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
945 {
946 /* Skip the remaining bytes in a header that is longer than
947 * we expected.
948 */
949 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
950 }
951
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700952 dprintf (SPEW, "=== Sparse Image Header ===\n");
953 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
954 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
955 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
956 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
957 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
958 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
959 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
960 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700961
962 /* Start processing chunks */
963 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
964 {
965 /* Read and skip over chunk header */
966 chunk_header = (chunk_header_t *) data;
967 data += sizeof(chunk_header_t);
968
969 dprintf (SPEW, "=== Chunk Header ===\n");
970 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
971 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
972 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
973
974 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
975 {
976 /* Skip the remaining bytes in a header that is longer than
977 * we expected.
978 */
979 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
980 }
981
982 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
983 switch (chunk_header->chunk_type)
984 {
985 case CHUNK_TYPE_RAW:
986 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
987 chunk_data_sz))
988 {
989 fastboot_fail("Bogus chunk size for chunk type Raw");
990 return;
991 }
992
Ajay Dudaniab18f022011-05-12 14:39:22 -0700993 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
994 chunk_data_sz,
995 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700996 {
997 fastboot_fail("flash write failure");
998 return;
999 }
1000 total_blocks += chunk_header->chunk_sz;
1001 data += chunk_data_sz;
1002 break;
1003
1004 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001005 total_blocks += chunk_header->chunk_sz;
1006 break;
1007
Ajay Dudani5c761132011-04-07 20:19:04 -07001008 case CHUNK_TYPE_CRC:
1009 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1010 {
1011 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1012 return;
1013 }
1014 total_blocks += chunk_header->chunk_sz;
1015 data += chunk_data_sz;
1016 break;
1017
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001018 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001019 fastboot_fail("Unknown chunk type");
1020 return;
1021 }
1022 }
1023
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001024 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1025 total_blocks, sparse_header->total_blks);
1026
1027 if(total_blocks != sparse_header->total_blks)
1028 {
1029 fastboot_fail("sparse image write failure");
1030 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001031
1032 fastboot_okay("");
1033 return;
1034}
1035
1036void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1037{
1038 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001039 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1040 unsigned int *magic_number = (unsigned int *) data;
1041 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001042
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001043 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
1044 magic_number[1] == SSD_HEADER_MAGIC_1)
1045 {
1046#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -07001047 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001048#endif
Greg Griscod6250552011-06-29 14:40:23 -07001049 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001050 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1051 return;
1052 }
1053 }
1054 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001055 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1056 cmd_flash_mmc_img(arg, data, sz);
1057 else
1058 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001059 return;
1060}
1061
Dima Zavin214cc642009-01-26 11:16:21 -08001062void cmd_flash(const char *arg, void *data, unsigned sz)
1063{
1064 struct ptentry *ptn;
1065 struct ptable *ptable;
1066 unsigned extra = 0;
1067
1068 ptable = flash_get_ptable();
1069 if (ptable == NULL) {
1070 fastboot_fail("partition table doesn't exist");
1071 return;
1072 }
1073
1074 ptn = ptable_find(ptable, arg);
1075 if (ptn == NULL) {
1076 fastboot_fail("unknown partition name");
1077 return;
1078 }
1079
1080 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1081 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1082 fastboot_fail("image is not a boot image");
1083 return;
1084 }
1085 }
1086
Chandan Uddarajud6d45042010-02-24 21:12:45 -08001087 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301088 || !strcmp(ptn->name, "persist")) {
1089 if (flash_ecc_bch_enabled())
1090 /* Spare data bytes for 8 bit ECC increased by 4 */
1091 extra = ((page_size >> 9) * 20);
1092 else
1093 extra = ((page_size >> 9) * 16);
1094 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001095 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001096
1097 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1098 if (flash_write(ptn, extra, data, sz)) {
1099 fastboot_fail("flash write failure");
1100 return;
1101 }
1102 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1103 fastboot_okay("");
1104}
1105
1106void cmd_continue(const char *arg, void *data, unsigned sz)
1107{
1108 fastboot_okay("");
1109 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001110 if (target_is_emmc_boot())
1111 {
1112 boot_linux_from_mmc();
1113 }
1114 else
1115 {
1116 boot_linux_from_flash();
1117 }
Dima Zavin214cc642009-01-26 11:16:21 -08001118}
1119
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001120void cmd_reboot(const char *arg, void *data, unsigned sz)
1121{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001122 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001123 fastboot_okay("");
1124 reboot_device(0);
1125}
1126
1127void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1128{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001129 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001130 fastboot_okay("");
1131 reboot_device(FASTBOOT_MODE);
1132}
1133
Shashank Mittal162244e2011-08-08 19:01:25 -07001134void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1135{
1136 if(!device.is_unlocked)
1137 {
1138 device.is_unlocked = 1;
1139 write_device_info(&device);
1140 }
1141 fastboot_okay("");
1142}
1143
Shashank Mittala0032282011-08-26 14:50:11 -07001144void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1145{
1146 char response[64];
1147 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1148 fastboot_info(response);
1149 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1150 fastboot_info(response);
1151 fastboot_okay("");
1152}
1153
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001154void splash_screen ()
1155{
1156 struct ptentry *ptn;
1157 struct ptable *ptable;
1158 struct fbcon_config *fb_display = NULL;
1159
1160 if (!target_is_emmc_boot())
1161 {
1162 ptable = flash_get_ptable();
1163 if (ptable == NULL) {
1164 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001165 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001166 }
1167
1168 ptn = ptable_find(ptable, "splash");
1169 if (ptn == NULL) {
1170 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1171 } else {
1172 fb_display = fbcon_display();
1173 if (fb_display) {
1174 if (flash_read(ptn, 0, fb_display->base,
1175 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1176 fbcon_clear();
1177 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1178 }
1179 }
1180 }
1181 }
1182}
1183
Brian Swetland9c4c0752009-01-25 16:23:50 -08001184void aboot_init(const struct app_descriptor *app)
1185{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001186 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001187 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001188 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001189
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001190 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001191 if (target_is_emmc_boot())
1192 {
1193 page_size = 2048;
1194 page_mask = page_size - 1;
1195 }
1196 else
1197 {
1198 page_size = flash_page_size();
1199 page_mask = page_size - 1;
1200 }
1201
Shashank Mittal162244e2011-08-08 19:01:25 -07001202 if(target_use_signed_kernel())
1203 {
1204 read_device_info(&device);
1205
Shashank Mittal162244e2011-08-08 19:01:25 -07001206 }
1207
Greg Griscod6250552011-06-29 14:40:23 -07001208 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001209 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001210 surf_udc_device.serialno = sn_buf;
1211
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001212 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001213 if (keys_get_state(KEY_HOME) != 0)
1214 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001215 if (keys_get_state(KEY_VOLUMEUP) != 0)
1216 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001217 if(!boot_into_recovery)
1218 {
1219 if (keys_get_state(KEY_BACK) != 0)
1220 goto fastboot;
1221 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1222 goto fastboot;
1223 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001224
1225 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001226 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001227 goto fastboot;
1228 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001229
Ajay Dudani77421292010-10-27 19:34:06 -07001230 reboot_mode = check_reboot_mode();
1231 if (reboot_mode == RECOVERY_MODE) {
1232 boot_into_recovery = 1;
1233 } else if(reboot_mode == FASTBOOT_MODE) {
1234 goto fastboot;
1235 }
1236
Shashank Mittal23b8f422010-04-16 19:27:21 -07001237 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001238 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001239 if(emmc_recovery_init())
1240 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001241 if(target_use_signed_kernel())
1242 {
1243 if((device.is_unlocked) || (device.is_tampered))
1244 {
1245 #ifdef TZ_TAMPER_FUSE
1246 set_tamper_fuse_cmd();
1247 #endif
1248 }
1249 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001250 boot_linux_from_mmc();
1251 }
1252 else
1253 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001254 recovery_init();
1255 boot_linux_from_flash();
1256 }
Dima Zavinb4283602009-01-26 16:36:57 -08001257 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1258 "to fastboot mode.\n");
1259
1260fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001261
Shashank Mittal162244e2011-08-08 19:01:25 -07001262 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001263
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001264 if(!usb_init)
1265 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001266
1267 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001268
Shashank Mittal23b8f422010-04-16 19:27:21 -07001269 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001270 {
1271 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001272 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001273 }
1274 else
1275 {
1276 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001277 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001278 }
1279
1280 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001281 fastboot_register("reboot", cmd_reboot);
1282 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001283 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001284 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001285 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001286 fastboot_publish("kernel", "lk");
Kinson Chikf1a43512011-07-14 11:28:39 -07001287 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001288 sz = target_get_max_flash_size();
1289 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001290 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001291}
1292
1293APP_START(aboot)
1294 .init = aboot_init,
1295APP_END
1296