blob: 10aac4679d7a0e40aeeea9395a3b7f4ae1f772b7 [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=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080077static const char *battchg_pause = " androidboot.mode=charger";
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
119 memcpy(atag_ptn.name, ptn->name, 16);
120 atag_ptn.name[15] = '\0';
121 atag_ptn.offset = ptn->start;
122 atag_ptn.size = ptn->length;
123 atag_ptn.flags = ptn->flags;
124 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
125 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
126}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700128void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129 const char *cmdline, unsigned machtype,
130 void *ramdisk, unsigned ramdisk_size)
131{
132 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800133 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800134 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800135 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800136 int cmdline_len = 0;
137 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700138 int pause_at_bootup = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800139 unsigned char *cmdline_final = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800140
141 /* CORE */
142 *ptr++ = 2;
143 *ptr++ = 0x54410001;
144
145 if (ramdisk_size) {
146 *ptr++ = 4;
147 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800148 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800149 *ptr++ = ramdisk_size;
150 }
151
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800152 ptr = target_atag_mem(ptr);
153
David Ng183a7422009-12-07 14:55:21 -0800154 if (!target_is_emmc_boot()) {
155 /* Skip NAND partition ATAGS for eMMC boot */
156 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
157 int i;
Mekala Natarajanc43d8842011-10-26 15:09:10 -0700158 *ptr++ = 2 + (ptable->count * (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
Shashank Mittalcd98d472011-08-02 14:29:24 -0700182 if(target_use_signed_kernel() && auth_kernel_img) {
183 cmdline_len += strlen(auth_kernel);
184 }
185
Ajay Dudanid04110c2011-01-17 23:55:07 -0800186 /* Determine correct androidboot.baseband to use */
187 switch(target_baseband())
188 {
189 case BASEBAND_APQ:
190 cmdline_len += strlen(baseband_apq);
191 break;
192
193 case BASEBAND_MSM:
194 cmdline_len += strlen(baseband_msm);
195 break;
196
197 case BASEBAND_CSFB:
198 cmdline_len += strlen(baseband_csfb);
199 break;
200
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800201 case BASEBAND_SVLTE2A:
202 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800203 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700204
205 case BASEBAND_MDM:
206 cmdline_len += strlen(baseband_mdm);
207 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800208 }
209
David Ng183a7422009-12-07 14:55:21 -0800210 if (cmdline_len > 0) {
211 const char *src;
212 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800213 unsigned n;
214 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800215 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800216 *ptr++ = (n / 4) + 2;
217 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800218 dst = (char *)ptr;
Amol Jadi168b7712012-03-06 16:15:00 -0800219 /* Save start ptr for debug print */
220 cmdline_final = (char *)ptr;
David Ng183a7422009-12-07 14:55:21 -0800221 if (have_cmdline) {
222 src = cmdline;
223 while ((*dst++ = *src++));
224 }
225 if (target_is_emmc_boot()) {
226 src = emmc_cmdline;
227 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700228 have_cmdline = 1;
229 while ((*dst++ = *src++));
230 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800231
232 src = usb_sn_cmdline;
233 if (have_cmdline) --dst;
234 have_cmdline = 1;
235 while ((*dst++ = *src++));
236 src = sn_buf;
237 if (have_cmdline) --dst;
238 have_cmdline = 1;
239 while ((*dst++ = *src++));
240
David Ngf773dde2010-07-26 19:55:08 -0700241 if (pause_at_bootup) {
242 src = battchg_pause;
243 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800244 while ((*dst++ = *src++));
245 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800246
Shashank Mittalcd98d472011-08-02 14:29:24 -0700247 if(target_use_signed_kernel() && auth_kernel_img) {
248 src = auth_kernel;
249 if (have_cmdline) --dst;
250 while ((*dst++ = *src++));
251 }
252
Ajay Dudanid04110c2011-01-17 23:55:07 -0800253 switch(target_baseband())
254 {
255 case BASEBAND_APQ:
256 src = baseband_apq;
257 if (have_cmdline) --dst;
258 while ((*dst++ = *src++));
259 break;
260
261 case BASEBAND_MSM:
262 src = baseband_msm;
263 if (have_cmdline) --dst;
264 while ((*dst++ = *src++));
265 break;
266
267 case BASEBAND_CSFB:
268 src = baseband_csfb;
269 if (have_cmdline) --dst;
270 while ((*dst++ = *src++));
271 break;
272
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800273 case BASEBAND_SVLTE2A:
274 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800275 if (have_cmdline) --dst;
276 while ((*dst++ = *src++));
277 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700278
279 case BASEBAND_MDM:
280 src = baseband_mdm;
281 if (have_cmdline) --dst;
282 while ((*dst++ = *src++));
283 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800284 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800285 ptr += (n / 4);
286 }
287
288 /* END */
289 *ptr++ = 0;
290 *ptr++ = 0;
291
292 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
293 kernel, ramdisk, ramdisk_size);
Amol Jadi168b7712012-03-06 16:15:00 -0800294 if (cmdline_final)
295 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800296
297 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700298 /* do any platform specific cleanup before kernel entry */
299 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800300 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800301 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800302 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800303}
304
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800305unsigned page_size = 0;
306unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800307
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800308#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800309
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800310static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800311
Shashank Mittal23b8f422010-04-16 19:27:21 -0700312int boot_linux_from_mmc(void)
313{
314 struct boot_img_hdr *hdr = (void*) buf;
315 struct boot_img_hdr *uhdr;
316 unsigned offset = 0;
317 unsigned long long ptn = 0;
318 unsigned n = 0;
319 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700320 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700321
Shashank Mittalcd98d472011-08-02 14:29:24 -0700322 unsigned char *image_addr = 0;
323 unsigned kernel_actual;
324 unsigned ramdisk_actual;
325 unsigned imagesize_actual;
326
Shashank Mittal23b8f422010-04-16 19:27:21 -0700327 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
328 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
329 dprintf(INFO, "Unified boot method!\n");
330 hdr = uhdr;
331 goto unified_boot;
332 }
Greg Griscod6250552011-06-29 14:40:23 -0700333 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700334 index = partition_get_index("boot");
335 ptn = partition_get_offset(index);
336 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700337 dprintf(CRITICAL, "ERROR: No boot partition found\n");
338 return -1;
339 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700340 }
341 else {
342 index = partition_get_index("recovery");
343 ptn = partition_get_offset(index);
344 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700345 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
346 return -1;
347 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700348 }
349
Greg Griscod6250552011-06-29 14:40:23 -0700350 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700351 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
352 return -1;
353 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700354
355 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700356 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700357 return -1;
358 }
359
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700360 if (hdr->page_size && (hdr->page_size != page_size)) {
361 page_size = hdr->page_size;
362 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700363 }
364
Shashank Mittalcd98d472011-08-02 14:29:24 -0700365 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700366 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800367 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700368 image_addr = (unsigned char *)target_get_scratch_address();
369 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
370 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
371 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
372
373 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700374
375 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700376 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700377
Shashank Mittalcd98d472011-08-02 14:29:24 -0700378 /* Read image without signature */
379 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
380 {
381 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
382 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800383 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700384
385 offset = imagesize_actual;
386 /* Read signature */
387 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
388 {
389 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
390 }
391 else
392 {
393 auth_kernel_img = image_verify((unsigned char *)image_addr,
394 (unsigned char *)(image_addr + imagesize_actual),
395 imagesize_actual,
396 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700397
398 if(auth_kernel_img)
399 {
400 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700401 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700402 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700403 }
404
405 /* Move kernel and ramdisk to correct address */
406 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
407 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700408
409 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700410 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700411 {
412 write_device_info_mmc(&device);
413 #ifdef TZ_TAMPER_FUSE
414 set_tamper_fuse_cmd();
415 #endif
416 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700417 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700418 else
419 {
420 offset += page_size;
421
422 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
423 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
424 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
425 return -1;
426 }
427 offset += n;
428
429 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
430 if(n != 0)
431 {
432 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
433 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
434 return -1;
435 }
436 }
437 offset += n;
438 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700439
440unified_boot:
441 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
442 hdr->kernel_size);
443 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
444 hdr->ramdisk_size);
445
446 if(hdr->cmdline[0]) {
447 cmdline = (char*) hdr->cmdline;
448 } else {
449 cmdline = DEFAULT_CMDLINE;
450 }
451 dprintf(INFO, "cmdline = '%s'\n", cmdline);
452
453 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700454 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700455 (const char *)cmdline, board_machtype(),
456 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
457
458 return 0;
459}
460
Dima Zavin214cc642009-01-26 11:16:21 -0800461int boot_linux_from_flash(void)
462{
463 struct boot_img_hdr *hdr = (void*) buf;
464 unsigned n;
465 struct ptentry *ptn;
466 struct ptable *ptable;
467 unsigned offset = 0;
468 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800469
Shashank Mittalcd98d472011-08-02 14:29:24 -0700470 unsigned char *image_addr = 0;
471 unsigned kernel_actual;
472 unsigned ramdisk_actual;
473 unsigned imagesize_actual;
474
David Ng183a7422009-12-07 14:55:21 -0800475 if (target_is_emmc_boot()) {
476 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
477 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
478 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
479 return -1;
480 }
481 goto continue_boot;
482 }
483
Dima Zavin214cc642009-01-26 11:16:21 -0800484 ptable = flash_get_ptable();
485 if (ptable == NULL) {
486 dprintf(CRITICAL, "ERROR: Partition table not found\n");
487 return -1;
488 }
489
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800490 if(!boot_into_recovery)
491 {
492 ptn = ptable_find(ptable, "boot");
493 if (ptn == NULL) {
494 dprintf(CRITICAL, "ERROR: No boot partition found\n");
495 return -1;
496 }
497 }
498 else
499 {
500 ptn = ptable_find(ptable, "recovery");
501 if (ptn == NULL) {
502 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
503 return -1;
504 }
Dima Zavin214cc642009-01-26 11:16:21 -0800505 }
506
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800507 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800508 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
509 return -1;
510 }
Dima Zavin214cc642009-01-26 11:16:21 -0800511
512 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700513 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800514 return -1;
515 }
516
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800517 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700518 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 -0800519 return -1;
520 }
521
Shashank Mittalcd98d472011-08-02 14:29:24 -0700522 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700523 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700524 {
525 image_addr = (unsigned char *)target_get_scratch_address();
526 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
527 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
528 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800529
Shashank Mittalcd98d472011-08-02 14:29:24 -0700530 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700531
532 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700533 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700534
Shashank Mittalcd98d472011-08-02 14:29:24 -0700535 /* Read image without signature */
536 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
537 {
538 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
539 return -1;
540 }
Dima Zavin214cc642009-01-26 11:16:21 -0800541
Shashank Mittalcd98d472011-08-02 14:29:24 -0700542 offset = imagesize_actual;
543 /* Read signature */
544 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
545 {
546 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
547 }
548 else
549 {
550
551 /* Verify signature */
552 auth_kernel_img = image_verify((unsigned char *)image_addr,
553 (unsigned char *)(image_addr + imagesize_actual),
554 imagesize_actual,
555 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700556
557 if(auth_kernel_img)
558 {
559 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700560 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700561 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700562 }
563
564 /* Move kernel and ramdisk to correct address */
565 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
566 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700567
568 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700569 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700570 {
571 write_device_info_flash(&device);
572 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530573#if USE_PCOM_SECBOOT
574 set_tamper_flag(device.is_tampered);
575#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700576 }
577 else
578 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700579 offset = page_size;
580
Shashank Mittalcd98d472011-08-02 14:29:24 -0700581 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
582 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
583 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
584 return -1;
585 }
586 offset += n;
587
588 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
589 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
590 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
591 return -1;
592 }
593 offset += n;
594 }
David Ng183a7422009-12-07 14:55:21 -0800595continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800596 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
597 hdr->kernel_size);
598 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
599 hdr->ramdisk_size);
600
601 if(hdr->cmdline[0]) {
602 cmdline = (char*) hdr->cmdline;
603 } else {
604 cmdline = DEFAULT_CMDLINE;
605 }
606 dprintf(INFO, "cmdline = '%s'\n", cmdline);
607
608 /* TODO: create/pass atags to kernel */
609
610 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700611 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800612 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800613 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
614
615 return 0;
616}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800617
Shashank Mittal162244e2011-08-08 19:01:25 -0700618unsigned char info_buf[4096];
619void write_device_info_mmc(device_info *dev)
620{
621 struct device_info *info = (void*) info_buf;
622 unsigned long long ptn = 0;
623 unsigned long long size;
624 int index = INVALID_PTN;
625
626 index = partition_get_index("aboot");
627 ptn = partition_get_offset(index);
628 if(ptn == 0)
629 {
630 return;
631 }
632
633 size = partition_get_size(index);
634
635 memcpy(info, dev, sizeof(device_info));
636
637 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
638 {
639 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
640 return;
641 }
642}
643
644void read_device_info_mmc(device_info *dev)
645{
646 struct device_info *info = (void*) info_buf;
647 unsigned long long ptn = 0;
648 unsigned long long size;
649 int index = INVALID_PTN;
650
651 index = partition_get_index("aboot");
652 ptn = partition_get_offset(index);
653 if(ptn == 0)
654 {
655 return;
656 }
657
658 size = partition_get_size(index);
659
660 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
661 {
662 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
663 return;
664 }
665
666 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
667 {
668 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
669 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700670 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700671
672 write_device_info_mmc(info);
673 }
674 memcpy(dev, info, sizeof(device_info));
675}
676
677void write_device_info_flash(device_info *dev)
678{
679 struct device_info *info = (void *) info_buf;
680 struct ptentry *ptn;
681 struct ptable *ptable;
682
683 ptable = flash_get_ptable();
684 if (ptable == NULL)
685 {
686 dprintf(CRITICAL, "ERROR: Partition table not found\n");
687 return;
688 }
689
690 ptn = ptable_find(ptable, "devinfo");
691 if (ptn == NULL)
692 {
693 dprintf(CRITICAL, "ERROR: No boot partition found\n");
694 return;
695 }
696
697 memcpy(info, dev, sizeof(device_info));
698
699 if (flash_write(ptn, 0, (void *)info_buf, page_size))
700 {
701 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
702 return;
703 }
704}
705
706void read_device_info_flash(device_info *dev)
707{
708 struct device_info *info = (void*) info_buf;
709 struct ptentry *ptn;
710 struct ptable *ptable;
711
712 ptable = flash_get_ptable();
713 if (ptable == NULL)
714 {
715 dprintf(CRITICAL, "ERROR: Partition table not found\n");
716 return;
717 }
718
719 ptn = ptable_find(ptable, "devinfo");
720 if (ptn == NULL)
721 {
722 dprintf(CRITICAL, "ERROR: No boot partition found\n");
723 return;
724 }
725
726 if (flash_read(ptn, 0, (void *)info_buf, page_size))
727 {
728 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
729 return;
730 }
731
732 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
733 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700734 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
735 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700736 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700737 write_device_info_flash(info);
738 }
739 memcpy(dev, info, sizeof(device_info));
740}
741
742void write_device_info(device_info *dev)
743{
744 if(target_is_emmc_boot())
745 {
746 write_device_info_mmc(dev);
747 }
748 else
749 {
750 write_device_info_flash(dev);
751 }
752}
753
754void read_device_info(device_info *dev)
755{
756 if(target_is_emmc_boot())
757 {
758 read_device_info_mmc(dev);
759 }
760 else
761 {
762 read_device_info_flash(dev);
763 }
764}
765
766void reset_device_info()
767{
768 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700769 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700770 write_device_info(&device);
771}
772
773void set_device_root()
774{
775 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700776 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700777 write_device_info(&device);
778}
779
Brian Swetland9c4c0752009-01-25 16:23:50 -0800780void cmd_boot(const char *arg, void *data, unsigned sz)
781{
782 unsigned kernel_actual;
783 unsigned ramdisk_actual;
784 static struct boot_img_hdr hdr;
785 char *ptr = ((char*) data);
786
787 if (sz < sizeof(hdr)) {
788 fastboot_fail("invalid bootimage header");
789 return;
790 }
791
792 memcpy(&hdr, data, sizeof(hdr));
793
794 /* ensure commandline is terminated */
795 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
796
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700797 if(target_is_emmc_boot() && hdr.page_size) {
798 page_size = hdr.page_size;
799 page_mask = page_size - 1;
800 }
801
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800802 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
803 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800804
Shashank Mittal1f0e2662011-09-01 15:06:00 -0700805 /* sz should have atleast raw boot image */
806 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800807 fastboot_fail("incomplete bootimage");
808 return;
809 }
810
Ajay Dudanie28a6072011-07-01 13:59:46 -0700811 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
812 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800813
814 fastboot_okay("");
815 udc_stop();
816
Amol Jadie67872e2011-06-27 14:14:11 -0700817 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800818 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700819 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800820}
821
Dima Zavin214cc642009-01-26 11:16:21 -0800822void cmd_erase(const char *arg, void *data, unsigned sz)
823{
824 struct ptentry *ptn;
825 struct ptable *ptable;
826
827 ptable = flash_get_ptable();
828 if (ptable == NULL) {
829 fastboot_fail("partition table doesn't exist");
830 return;
831 }
832
833 ptn = ptable_find(ptable, arg);
834 if (ptn == NULL) {
835 fastboot_fail("unknown partition name");
836 return;
837 }
838
839 if (flash_erase(ptn)) {
840 fastboot_fail("failed to erase partition");
841 return;
842 }
843 fastboot_okay("");
844}
845
Bikas Gurungd48bd242010-09-04 19:54:32 -0700846
847void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
848{
849 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -0800850 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -0700851 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700852
Kinson Chikf1a43512011-07-14 11:28:39 -0700853 index = partition_get_index(arg);
854 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -0700855
Kinson Chikf1a43512011-07-14 11:28:39 -0700856 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -0700857 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700858 return;
859 }
neetidb4b24d62012-01-20 12:13:09 -0800860 /* Simple inefficient version of erase. Just writing
861 0 in first block */
862 if (mmc_write(ptn , 512, (unsigned int *)out)) {
863 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700864 return;
865 }
866 fastboot_okay("");
867}
868
869
Ajay Dudani5c761132011-04-07 20:19:04 -0700870void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700871{
872 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700873 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700874 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700875
Greg Grisco6e754772011-06-23 12:19:39 -0700876 if (!strcmp(arg, "partition"))
877 {
878 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -0700879 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -0700880 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700881 return;
882 }
883 }
Greg Grisco6e754772011-06-23 12:19:39 -0700884 else
885 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700886 index = partition_get_index(arg);
887 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700888 if(ptn == 0) {
889 fastboot_fail("partition table doesn't exist");
890 return;
891 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700892
Greg Grisco6e754772011-06-23 12:19:39 -0700893 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
894 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
895 fastboot_fail("image is not a boot image");
896 return;
897 }
898 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700899
Kinson Chikf1a43512011-07-14 11:28:39 -0700900 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700901 if (ROUND_TO_PAGE(sz,511) > size) {
902 fastboot_fail("size too large");
903 return;
904 }
905 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
906 fastboot_fail("flash write failure");
907 return;
908 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700909 }
910 fastboot_okay("");
911 return;
912}
913
Ajay Dudani5c761132011-04-07 20:19:04 -0700914void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
915{
916 unsigned int chunk;
917 unsigned int chunk_data_sz;
918 sparse_header_t *sparse_header;
919 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700920 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700921 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530922 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700923 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700924
Kinson Chikf1a43512011-07-14 11:28:39 -0700925 index = partition_get_index(arg);
926 ptn = partition_get_offset(index);
927 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700928 fastboot_fail("partition table doesn't exist");
929 return;
930 }
931
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530932 size = partition_get_size(index);
933 if (ROUND_TO_PAGE(sz,511) > size) {
934 fastboot_fail("size too large");
935 return;
936 }
937
Ajay Dudani5c761132011-04-07 20:19:04 -0700938 /* Read and skip over sparse image header */
939 sparse_header = (sparse_header_t *) data;
940 data += sparse_header->file_hdr_sz;
941 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
942 {
943 /* Skip the remaining bytes in a header that is longer than
944 * we expected.
945 */
946 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
947 }
948
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700949 dprintf (SPEW, "=== Sparse Image Header ===\n");
950 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
951 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
952 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
953 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
954 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
955 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
956 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
957 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700958
959 /* Start processing chunks */
960 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
961 {
962 /* Read and skip over chunk header */
963 chunk_header = (chunk_header_t *) data;
964 data += sizeof(chunk_header_t);
965
966 dprintf (SPEW, "=== Chunk Header ===\n");
967 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
968 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
969 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
970
971 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
972 {
973 /* Skip the remaining bytes in a header that is longer than
974 * we expected.
975 */
976 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
977 }
978
979 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
980 switch (chunk_header->chunk_type)
981 {
982 case CHUNK_TYPE_RAW:
983 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
984 chunk_data_sz))
985 {
986 fastboot_fail("Bogus chunk size for chunk type Raw");
987 return;
988 }
989
Ajay Dudaniab18f022011-05-12 14:39:22 -0700990 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
991 chunk_data_sz,
992 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700993 {
994 fastboot_fail("flash write failure");
995 return;
996 }
997 total_blocks += chunk_header->chunk_sz;
998 data += chunk_data_sz;
999 break;
1000
1001 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001002 total_blocks += chunk_header->chunk_sz;
1003 break;
1004
Ajay Dudani5c761132011-04-07 20:19:04 -07001005 case CHUNK_TYPE_CRC:
1006 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1007 {
1008 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1009 return;
1010 }
1011 total_blocks += chunk_header->chunk_sz;
1012 data += chunk_data_sz;
1013 break;
1014
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001015 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001016 fastboot_fail("Unknown chunk type");
1017 return;
1018 }
1019 }
1020
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001021 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1022 total_blocks, sparse_header->total_blks);
1023
1024 if(total_blocks != sparse_header->total_blks)
1025 {
1026 fastboot_fail("sparse image write failure");
1027 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001028
1029 fastboot_okay("");
1030 return;
1031}
1032
1033void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1034{
1035 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001036 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1037 unsigned int *magic_number = (unsigned int *) data;
1038 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001039
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001040 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
1041 magic_number[1] == SSD_HEADER_MAGIC_1)
1042 {
1043#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -07001044 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001045#endif
Greg Griscod6250552011-06-29 14:40:23 -07001046 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001047 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1048 return;
1049 }
1050 }
1051 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001052 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1053 cmd_flash_mmc_img(arg, data, sz);
1054 else
1055 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001056 return;
1057}
1058
Dima Zavin214cc642009-01-26 11:16:21 -08001059void cmd_flash(const char *arg, void *data, unsigned sz)
1060{
1061 struct ptentry *ptn;
1062 struct ptable *ptable;
1063 unsigned extra = 0;
1064
1065 ptable = flash_get_ptable();
1066 if (ptable == NULL) {
1067 fastboot_fail("partition table doesn't exist");
1068 return;
1069 }
1070
1071 ptn = ptable_find(ptable, arg);
1072 if (ptn == NULL) {
1073 fastboot_fail("unknown partition name");
1074 return;
1075 }
1076
1077 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1078 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1079 fastboot_fail("image is not a boot image");
1080 return;
1081 }
1082 }
1083
Chandan Uddarajud6d45042010-02-24 21:12:45 -08001084 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301085 || !strcmp(ptn->name, "persist")) {
1086 if (flash_ecc_bch_enabled())
1087 /* Spare data bytes for 8 bit ECC increased by 4 */
1088 extra = ((page_size >> 9) * 20);
1089 else
1090 extra = ((page_size >> 9) * 16);
1091 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001092 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001093
1094 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1095 if (flash_write(ptn, extra, data, sz)) {
1096 fastboot_fail("flash write failure");
1097 return;
1098 }
1099 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1100 fastboot_okay("");
1101}
1102
1103void cmd_continue(const char *arg, void *data, unsigned sz)
1104{
1105 fastboot_okay("");
1106 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001107 if (target_is_emmc_boot())
1108 {
1109 boot_linux_from_mmc();
1110 }
1111 else
1112 {
1113 boot_linux_from_flash();
1114 }
Dima Zavin214cc642009-01-26 11:16:21 -08001115}
1116
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001117void cmd_reboot(const char *arg, void *data, unsigned sz)
1118{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001119 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001120 fastboot_okay("");
1121 reboot_device(0);
1122}
1123
1124void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1125{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001126 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001127 fastboot_okay("");
1128 reboot_device(FASTBOOT_MODE);
1129}
1130
Shashank Mittal162244e2011-08-08 19:01:25 -07001131void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1132{
1133 if(!device.is_unlocked)
1134 {
1135 device.is_unlocked = 1;
1136 write_device_info(&device);
1137 }
1138 fastboot_okay("");
1139}
1140
Shashank Mittala0032282011-08-26 14:50:11 -07001141void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1142{
1143 char response[64];
1144 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1145 fastboot_info(response);
1146 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1147 fastboot_info(response);
1148 fastboot_okay("");
1149}
1150
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001151void splash_screen ()
1152{
1153 struct ptentry *ptn;
1154 struct ptable *ptable;
1155 struct fbcon_config *fb_display = NULL;
1156
1157 if (!target_is_emmc_boot())
1158 {
1159 ptable = flash_get_ptable();
1160 if (ptable == NULL) {
1161 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001162 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001163 }
1164
1165 ptn = ptable_find(ptable, "splash");
1166 if (ptn == NULL) {
1167 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1168 } else {
1169 fb_display = fbcon_display();
1170 if (fb_display) {
1171 if (flash_read(ptn, 0, fb_display->base,
1172 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1173 fbcon_clear();
1174 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1175 }
1176 }
1177 }
1178 }
1179}
1180
Brian Swetland9c4c0752009-01-25 16:23:50 -08001181void aboot_init(const struct app_descriptor *app)
1182{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001183 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001184 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001185 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001186
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001187 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001188 if (target_is_emmc_boot())
1189 {
1190 page_size = 2048;
1191 page_mask = page_size - 1;
1192 }
1193 else
1194 {
1195 page_size = flash_page_size();
1196 page_mask = page_size - 1;
1197 }
1198
Shashank Mittal162244e2011-08-08 19:01:25 -07001199 if(target_use_signed_kernel())
1200 {
1201 read_device_info(&device);
1202
Shashank Mittal162244e2011-08-08 19:01:25 -07001203 }
1204
Greg Griscod6250552011-06-29 14:40:23 -07001205 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001206 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001207 surf_udc_device.serialno = sn_buf;
1208
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001209 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001210 if (keys_get_state(KEY_HOME) != 0)
1211 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001212 if (keys_get_state(KEY_VOLUMEUP) != 0)
1213 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001214 if(!boot_into_recovery)
1215 {
1216 if (keys_get_state(KEY_BACK) != 0)
1217 goto fastboot;
1218 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1219 goto fastboot;
1220 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001221
1222 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001223 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001224 goto fastboot;
1225 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001226
Ajay Dudani77421292010-10-27 19:34:06 -07001227 reboot_mode = check_reboot_mode();
1228 if (reboot_mode == RECOVERY_MODE) {
1229 boot_into_recovery = 1;
1230 } else if(reboot_mode == FASTBOOT_MODE) {
1231 goto fastboot;
1232 }
1233
Shashank Mittal23b8f422010-04-16 19:27:21 -07001234 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001235 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001236 if(emmc_recovery_init())
1237 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001238 if(target_use_signed_kernel())
1239 {
1240 if((device.is_unlocked) || (device.is_tampered))
1241 {
1242 #ifdef TZ_TAMPER_FUSE
1243 set_tamper_fuse_cmd();
1244 #endif
1245 }
1246 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001247 boot_linux_from_mmc();
1248 }
1249 else
1250 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001251 recovery_init();
1252 boot_linux_from_flash();
1253 }
Dima Zavinb4283602009-01-26 16:36:57 -08001254 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1255 "to fastboot mode.\n");
1256
1257fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001258
Shashank Mittal162244e2011-08-08 19:01:25 -07001259 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001260
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001261 if(!usb_init)
1262 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001263
1264 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001265
Shashank Mittal23b8f422010-04-16 19:27:21 -07001266 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001267 {
1268 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001269 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001270 }
1271 else
1272 {
1273 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001274 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001275 }
1276
1277 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001278 fastboot_register("reboot", cmd_reboot);
1279 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001280 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001281 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001282 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001283 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001284 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001285 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001286 sz = target_get_max_flash_size();
1287 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001288 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001289}
1290
1291APP_START(aboot)
1292 .init = aboot_init,
1293APP_END
1294