blob: 45c04d5c727b2cfd2c0d460e833b74825214115b [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Neeti Desaie245d492012-06-01 12:52:13 -07005 * Copyright (c) 2009-2012, 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";
Amol Jadi5c61a952012-05-04 17:05:35 -070085static const char *baseband_sglte = " androidboot.baseband=sglte";
Ajay Dudanid04110c2011-01-17 23:55:07 -080086
Shashank Mittalcd98d472011-08-02 14:29:24 -070087/* Assuming unauthorized kernel image by default */
88static int auth_kernel_img = 0;
89
Shashank Mittal162244e2011-08-08 19:01:25 -070090static device_info device = {DEVICE_MAGIC, 0, 0};
91
Brian Swetland9c4c0752009-01-25 16:23:50 -080092static struct udc_device surf_udc_device = {
93 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080094 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080095 .version_id = 0x0100,
96 .manufacturer = "Google",
97 .product = "Android",
98};
99
Dima Zavin42168f22009-01-30 11:52:22 -0800100struct atag_ptbl_entry
101{
102 char name[16];
103 unsigned offset;
104 unsigned size;
105 unsigned flags;
106};
107
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800108char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700109
Greg Griscod2471ef2011-07-14 13:00:42 -0700110extern int emmc_recovery_init(void);
111
Kinson Chik0b1c8162011-08-31 16:31:57 -0700112#if NO_KEYPAD_DRIVER
113extern int fastboot_trigger(void);
114#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700115
Dima Zavin42168f22009-01-30 11:52:22 -0800116static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
117{
118 struct atag_ptbl_entry atag_ptn;
119
120 memcpy(atag_ptn.name, ptn->name, 16);
121 atag_ptn.name[15] = '\0';
122 atag_ptn.offset = ptn->start;
123 atag_ptn.size = ptn->length;
124 atag_ptn.flags = ptn->flags;
125 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
126 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
127}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800128
Neeti Desaie245d492012-06-01 12:52:13 -0700129unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800130{
David Ng183a7422009-12-07 14:55:21 -0800131 int cmdline_len = 0;
132 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800133 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700134 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800135
Brian Swetland9c4c0752009-01-25 16:23:50 -0800136 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800137 cmdline_len = strlen(cmdline);
138 have_cmdline = 1;
139 }
140 if (target_is_emmc_boot()) {
141 cmdline_len += strlen(emmc_cmdline);
142 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800143
144 cmdline_len += strlen(usb_sn_cmdline);
145 cmdline_len += strlen(sn_buf);
146
David Ngf773dde2010-07-26 19:55:08 -0700147 if (target_pause_for_battery_charge()) {
148 pause_at_bootup = 1;
149 cmdline_len += strlen(battchg_pause);
150 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800151
Shashank Mittalcd98d472011-08-02 14:29:24 -0700152 if(target_use_signed_kernel() && auth_kernel_img) {
153 cmdline_len += strlen(auth_kernel);
154 }
155
Ajay Dudanid04110c2011-01-17 23:55:07 -0800156 /* Determine correct androidboot.baseband to use */
157 switch(target_baseband())
158 {
159 case BASEBAND_APQ:
160 cmdline_len += strlen(baseband_apq);
161 break;
162
163 case BASEBAND_MSM:
164 cmdline_len += strlen(baseband_msm);
165 break;
166
167 case BASEBAND_CSFB:
168 cmdline_len += strlen(baseband_csfb);
169 break;
170
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800171 case BASEBAND_SVLTE2A:
172 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800173 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700174
175 case BASEBAND_MDM:
176 cmdline_len += strlen(baseband_mdm);
177 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700178
179 case BASEBAND_SGLTE:
180 cmdline_len += strlen(baseband_sglte);
181 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800182 }
183
David Ng183a7422009-12-07 14:55:21 -0800184 if (cmdline_len > 0) {
185 const char *src;
Neeti Desaie245d492012-06-01 12:52:13 -0700186 char *dst = malloc((cmdline_len + 4) & (~3));
187 assert(dst != NULL);
188
Amol Jadi168b7712012-03-06 16:15:00 -0800189 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700190 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800191 if (have_cmdline) {
192 src = cmdline;
193 while ((*dst++ = *src++));
194 }
195 if (target_is_emmc_boot()) {
196 src = emmc_cmdline;
197 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700198 have_cmdline = 1;
199 while ((*dst++ = *src++));
200 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800201
202 src = usb_sn_cmdline;
203 if (have_cmdline) --dst;
204 have_cmdline = 1;
205 while ((*dst++ = *src++));
206 src = sn_buf;
207 if (have_cmdline) --dst;
208 have_cmdline = 1;
209 while ((*dst++ = *src++));
210
David Ngf773dde2010-07-26 19:55:08 -0700211 if (pause_at_bootup) {
212 src = battchg_pause;
213 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800214 while ((*dst++ = *src++));
215 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800216
Shashank Mittalcd98d472011-08-02 14:29:24 -0700217 if(target_use_signed_kernel() && auth_kernel_img) {
218 src = auth_kernel;
219 if (have_cmdline) --dst;
220 while ((*dst++ = *src++));
221 }
222
Ajay Dudanid04110c2011-01-17 23:55:07 -0800223 switch(target_baseband())
224 {
225 case BASEBAND_APQ:
226 src = baseband_apq;
227 if (have_cmdline) --dst;
228 while ((*dst++ = *src++));
229 break;
230
231 case BASEBAND_MSM:
232 src = baseband_msm;
233 if (have_cmdline) --dst;
234 while ((*dst++ = *src++));
235 break;
236
237 case BASEBAND_CSFB:
238 src = baseband_csfb;
239 if (have_cmdline) --dst;
240 while ((*dst++ = *src++));
241 break;
242
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800243 case BASEBAND_SVLTE2A:
244 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800245 if (have_cmdline) --dst;
246 while ((*dst++ = *src++));
247 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700248
249 case BASEBAND_MDM:
250 src = baseband_mdm;
251 if (have_cmdline) --dst;
252 while ((*dst++ = *src++));
253 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700254
255 case BASEBAND_SGLTE:
256 src = baseband_sglte;
257 if (have_cmdline) --dst;
258 while ((*dst++ = *src++));
259 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800260 }
Neeti Desaie245d492012-06-01 12:52:13 -0700261 }
262 return cmdline_final;
263}
264
265unsigned *atag_core(unsigned *ptr)
266{
267 /* CORE */
268 *ptr++ = 2;
269 *ptr++ = 0x54410001;
270
271 return ptr;
272
273}
274
275unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
276 unsigned ramdisk_size)
277{
278 if (ramdisk_size) {
279 *ptr++ = 4;
280 *ptr++ = 0x54420005;
281 *ptr++ = (unsigned)ramdisk;
282 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800283 }
284
Neeti Desaie245d492012-06-01 12:52:13 -0700285 return ptr;
286}
287
288unsigned *atag_ptable(unsigned **ptr_addr)
289{
290 int i;
291 struct ptable *ptable;
292
293 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
294 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
295 sizeof(unsigned)));
296 *(*ptr_addr)++ = 0x4d534d70;
297 for (i = 0; i < ptable->count; ++i)
298 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
299 }
300
301 return (*ptr_addr);
302}
303
304unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
305{
306 int cmdline_length = 0;
307 int n;
308 unsigned char *cmdline_final = NULL;
309 char *dest;
310
311 cmdline_final = update_cmdline(cmdline);
312 if (cmdline_final){
313 dprintf(INFO, "cmdline: %s\n", cmdline_final);
314 }
315
316 cmdline_length =strlen(cmdline_final);
317 n = (cmdline_length + 4) & (~3);
318
319 *ptr++ = (n / 4) + 2;
320 *ptr++ = 0x54410009;
321 dest = (char *) ptr;
322 while (*dest++ = *cmdline_final++);
323 ptr += (n / 4);
324
325 return ptr;
326}
327
328unsigned *atag_end(unsigned *ptr)
329{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800330 /* END */
331 *ptr++ = 0;
332 *ptr++ = 0;
333
Neeti Desaie245d492012-06-01 12:52:13 -0700334 return ptr;
335}
336
337void generate_atags(unsigned *ptr, const char *cmdline,
338 void *ramdisk, unsigned ramdisk_size)
339{
340
341 ptr = atag_core(ptr);
342 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
343 ptr = target_atag_mem(ptr);
344
345 /* Skip NAND partition ATAGS for eMMC boot */
346 if (!target_is_emmc_boot()){
347 ptr = atag_ptable(&ptr);
348 }
349
350 ptr = atag_cmdline(ptr, cmdline);
351 ptr = atag_end(ptr);
352}
353
354void boot_linux(void *kernel, unsigned *tags,
355 const char *cmdline, unsigned machtype,
356 void *ramdisk, unsigned ramdisk_size)
357{
358 void (*entry)(unsigned, unsigned, unsigned*) = kernel;
359
360 /* Generating the Atags */
361 generate_atags(tags, cmdline, ramdisk, ramdisk_size);
362
Brian Swetland9c4c0752009-01-25 16:23:50 -0800363 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
364 kernel, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800365
366 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700367 /* do any platform specific cleanup before kernel entry */
368 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800369 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800370 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800371 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800372}
373
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800374unsigned page_size = 0;
375unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800376
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800377#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800378
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800379static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800380
Shashank Mittal23b8f422010-04-16 19:27:21 -0700381int boot_linux_from_mmc(void)
382{
383 struct boot_img_hdr *hdr = (void*) buf;
384 struct boot_img_hdr *uhdr;
385 unsigned offset = 0;
386 unsigned long long ptn = 0;
387 unsigned n = 0;
388 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700389 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700390
Shashank Mittalcd98d472011-08-02 14:29:24 -0700391 unsigned char *image_addr = 0;
392 unsigned kernel_actual;
393 unsigned ramdisk_actual;
394 unsigned imagesize_actual;
395
Shashank Mittal23b8f422010-04-16 19:27:21 -0700396 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
397 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
398 dprintf(INFO, "Unified boot method!\n");
399 hdr = uhdr;
400 goto unified_boot;
401 }
Greg Griscod6250552011-06-29 14:40:23 -0700402 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700403 index = partition_get_index("boot");
404 ptn = partition_get_offset(index);
405 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700406 dprintf(CRITICAL, "ERROR: No boot partition found\n");
407 return -1;
408 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700409 }
410 else {
411 index = partition_get_index("recovery");
412 ptn = partition_get_offset(index);
413 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700414 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
415 return -1;
416 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700417 }
418
Greg Griscod6250552011-06-29 14:40:23 -0700419 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700420 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
421 return -1;
422 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700423
424 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700425 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700426 return -1;
427 }
428
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700429 if (hdr->page_size && (hdr->page_size != page_size)) {
430 page_size = hdr->page_size;
431 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700432 }
433
Shashank Mittalcd98d472011-08-02 14:29:24 -0700434 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700435 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800436 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700437 image_addr = (unsigned char *)target_get_scratch_address();
438 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
439 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
440 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
441
442 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700443
444 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700445 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700446
Shashank Mittalcd98d472011-08-02 14:29:24 -0700447 /* Read image without signature */
448 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
449 {
450 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
451 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800452 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700453
454 offset = imagesize_actual;
455 /* Read signature */
456 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
457 {
458 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
459 }
460 else
461 {
462 auth_kernel_img = image_verify((unsigned char *)image_addr,
463 (unsigned char *)(image_addr + imagesize_actual),
464 imagesize_actual,
465 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700466
467 if(auth_kernel_img)
468 {
469 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700470 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700471 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700472 }
473
474 /* Move kernel and ramdisk to correct address */
475 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
476 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700477
478 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700479 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700480 {
481 write_device_info_mmc(&device);
482 #ifdef TZ_TAMPER_FUSE
483 set_tamper_fuse_cmd();
484 #endif
485 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530486 #if USE_PCOM_SECBOOT
487 set_tamper_flag(device.is_tampered);
488 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700489 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700490 else
491 {
492 offset += page_size;
493
494 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
495 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
496 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
497 return -1;
498 }
499 offset += n;
500
501 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
502 if(n != 0)
503 {
504 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
505 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
506 return -1;
507 }
508 }
509 offset += n;
510 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700511
512unified_boot:
513 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
514 hdr->kernel_size);
515 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
516 hdr->ramdisk_size);
517
518 if(hdr->cmdline[0]) {
519 cmdline = (char*) hdr->cmdline;
520 } else {
521 cmdline = DEFAULT_CMDLINE;
522 }
523 dprintf(INFO, "cmdline = '%s'\n", cmdline);
524
525 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700526 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700527 (const char *)cmdline, board_machtype(),
528 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
529
530 return 0;
531}
532
Dima Zavin214cc642009-01-26 11:16:21 -0800533int boot_linux_from_flash(void)
534{
535 struct boot_img_hdr *hdr = (void*) buf;
536 unsigned n;
537 struct ptentry *ptn;
538 struct ptable *ptable;
539 unsigned offset = 0;
540 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800541
Shashank Mittalcd98d472011-08-02 14:29:24 -0700542 unsigned char *image_addr = 0;
543 unsigned kernel_actual;
544 unsigned ramdisk_actual;
545 unsigned imagesize_actual;
546
David Ng183a7422009-12-07 14:55:21 -0800547 if (target_is_emmc_boot()) {
548 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
549 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
550 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
551 return -1;
552 }
553 goto continue_boot;
554 }
555
Dima Zavin214cc642009-01-26 11:16:21 -0800556 ptable = flash_get_ptable();
557 if (ptable == NULL) {
558 dprintf(CRITICAL, "ERROR: Partition table not found\n");
559 return -1;
560 }
561
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800562 if(!boot_into_recovery)
563 {
564 ptn = ptable_find(ptable, "boot");
565 if (ptn == NULL) {
566 dprintf(CRITICAL, "ERROR: No boot partition found\n");
567 return -1;
568 }
569 }
570 else
571 {
572 ptn = ptable_find(ptable, "recovery");
573 if (ptn == NULL) {
574 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
575 return -1;
576 }
Dima Zavin214cc642009-01-26 11:16:21 -0800577 }
578
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800579 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800580 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
581 return -1;
582 }
Dima Zavin214cc642009-01-26 11:16:21 -0800583
584 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700585 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800586 return -1;
587 }
588
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800589 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700590 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 -0800591 return -1;
592 }
593
Shashank Mittalcd98d472011-08-02 14:29:24 -0700594 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700595 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700596 {
597 image_addr = (unsigned char *)target_get_scratch_address();
598 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
599 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
600 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800601
Shashank Mittalcd98d472011-08-02 14:29:24 -0700602 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700603
604 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700605 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700606
Shashank Mittalcd98d472011-08-02 14:29:24 -0700607 /* Read image without signature */
608 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
609 {
610 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
611 return -1;
612 }
Dima Zavin214cc642009-01-26 11:16:21 -0800613
Shashank Mittalcd98d472011-08-02 14:29:24 -0700614 offset = imagesize_actual;
615 /* Read signature */
616 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
617 {
618 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
619 }
620 else
621 {
622
623 /* Verify signature */
624 auth_kernel_img = image_verify((unsigned char *)image_addr,
625 (unsigned char *)(image_addr + imagesize_actual),
626 imagesize_actual,
627 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700628
629 if(auth_kernel_img)
630 {
631 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700632 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700633 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700634 }
635
636 /* Move kernel and ramdisk to correct address */
637 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
638 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700639
640 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700641 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700642 {
643 write_device_info_flash(&device);
644 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530645#if USE_PCOM_SECBOOT
646 set_tamper_flag(device.is_tampered);
647#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700648 }
649 else
650 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700651 offset = page_size;
652
Shashank Mittalcd98d472011-08-02 14:29:24 -0700653 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
654 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
655 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
656 return -1;
657 }
658 offset += n;
659
660 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
661 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
662 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
663 return -1;
664 }
665 offset += n;
666 }
David Ng183a7422009-12-07 14:55:21 -0800667continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800668 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
669 hdr->kernel_size);
670 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
671 hdr->ramdisk_size);
672
673 if(hdr->cmdline[0]) {
674 cmdline = (char*) hdr->cmdline;
675 } else {
676 cmdline = DEFAULT_CMDLINE;
677 }
678 dprintf(INFO, "cmdline = '%s'\n", cmdline);
679
680 /* TODO: create/pass atags to kernel */
681
682 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700683 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800684 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800685 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
686
687 return 0;
688}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800689
Shashank Mittal162244e2011-08-08 19:01:25 -0700690unsigned char info_buf[4096];
691void write_device_info_mmc(device_info *dev)
692{
693 struct device_info *info = (void*) info_buf;
694 unsigned long long ptn = 0;
695 unsigned long long size;
696 int index = INVALID_PTN;
697
698 index = partition_get_index("aboot");
699 ptn = partition_get_offset(index);
700 if(ptn == 0)
701 {
702 return;
703 }
704
705 size = partition_get_size(index);
706
707 memcpy(info, dev, sizeof(device_info));
708
709 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
710 {
711 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
712 return;
713 }
714}
715
716void read_device_info_mmc(device_info *dev)
717{
718 struct device_info *info = (void*) info_buf;
719 unsigned long long ptn = 0;
720 unsigned long long size;
721 int index = INVALID_PTN;
722
723 index = partition_get_index("aboot");
724 ptn = partition_get_offset(index);
725 if(ptn == 0)
726 {
727 return;
728 }
729
730 size = partition_get_size(index);
731
732 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
733 {
734 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
735 return;
736 }
737
738 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
739 {
740 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
741 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700742 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700743
744 write_device_info_mmc(info);
745 }
746 memcpy(dev, info, sizeof(device_info));
747}
748
749void write_device_info_flash(device_info *dev)
750{
751 struct device_info *info = (void *) info_buf;
752 struct ptentry *ptn;
753 struct ptable *ptable;
754
755 ptable = flash_get_ptable();
756 if (ptable == NULL)
757 {
758 dprintf(CRITICAL, "ERROR: Partition table not found\n");
759 return;
760 }
761
762 ptn = ptable_find(ptable, "devinfo");
763 if (ptn == NULL)
764 {
765 dprintf(CRITICAL, "ERROR: No boot partition found\n");
766 return;
767 }
768
769 memcpy(info, dev, sizeof(device_info));
770
771 if (flash_write(ptn, 0, (void *)info_buf, page_size))
772 {
773 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
774 return;
775 }
776}
777
778void read_device_info_flash(device_info *dev)
779{
780 struct device_info *info = (void*) info_buf;
781 struct ptentry *ptn;
782 struct ptable *ptable;
783
784 ptable = flash_get_ptable();
785 if (ptable == NULL)
786 {
787 dprintf(CRITICAL, "ERROR: Partition table not found\n");
788 return;
789 }
790
791 ptn = ptable_find(ptable, "devinfo");
792 if (ptn == NULL)
793 {
794 dprintf(CRITICAL, "ERROR: No boot partition found\n");
795 return;
796 }
797
798 if (flash_read(ptn, 0, (void *)info_buf, page_size))
799 {
800 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
801 return;
802 }
803
804 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
805 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700806 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
807 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700808 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700809 write_device_info_flash(info);
810 }
811 memcpy(dev, info, sizeof(device_info));
812}
813
814void write_device_info(device_info *dev)
815{
816 if(target_is_emmc_boot())
817 {
818 write_device_info_mmc(dev);
819 }
820 else
821 {
822 write_device_info_flash(dev);
823 }
824}
825
826void read_device_info(device_info *dev)
827{
828 if(target_is_emmc_boot())
829 {
830 read_device_info_mmc(dev);
831 }
832 else
833 {
834 read_device_info_flash(dev);
835 }
836}
837
838void reset_device_info()
839{
840 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700841 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700842 write_device_info(&device);
843}
844
845void set_device_root()
846{
847 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700848 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700849 write_device_info(&device);
850}
851
Brian Swetland9c4c0752009-01-25 16:23:50 -0800852void cmd_boot(const char *arg, void *data, unsigned sz)
853{
854 unsigned kernel_actual;
855 unsigned ramdisk_actual;
856 static struct boot_img_hdr hdr;
857 char *ptr = ((char*) data);
858
859 if (sz < sizeof(hdr)) {
860 fastboot_fail("invalid bootimage header");
861 return;
862 }
863
864 memcpy(&hdr, data, sizeof(hdr));
865
866 /* ensure commandline is terminated */
867 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
868
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700869 if(target_is_emmc_boot() && hdr.page_size) {
870 page_size = hdr.page_size;
871 page_mask = page_size - 1;
872 }
873
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800874 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
875 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800876
Shashank Mittal1f0e2662011-09-01 15:06:00 -0700877 /* sz should have atleast raw boot image */
878 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800879 fastboot_fail("incomplete bootimage");
880 return;
881 }
882
Ajay Dudanie28a6072011-07-01 13:59:46 -0700883 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
884 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800885
886 fastboot_okay("");
887 udc_stop();
888
Amol Jadie67872e2011-06-27 14:14:11 -0700889 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800890 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700891 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800892}
893
Dima Zavin214cc642009-01-26 11:16:21 -0800894void cmd_erase(const char *arg, void *data, unsigned sz)
895{
896 struct ptentry *ptn;
897 struct ptable *ptable;
898
899 ptable = flash_get_ptable();
900 if (ptable == NULL) {
901 fastboot_fail("partition table doesn't exist");
902 return;
903 }
904
905 ptn = ptable_find(ptable, arg);
906 if (ptn == NULL) {
907 fastboot_fail("unknown partition name");
908 return;
909 }
910
911 if (flash_erase(ptn)) {
912 fastboot_fail("failed to erase partition");
913 return;
914 }
915 fastboot_okay("");
916}
917
Bikas Gurungd48bd242010-09-04 19:54:32 -0700918
919void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
920{
921 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -0800922 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -0700923 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700924
Kinson Chikf1a43512011-07-14 11:28:39 -0700925 index = partition_get_index(arg);
926 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -0700927
Kinson Chikf1a43512011-07-14 11:28:39 -0700928 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -0700929 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700930 return;
931 }
neetidb4b24d62012-01-20 12:13:09 -0800932 /* Simple inefficient version of erase. Just writing
933 0 in first block */
934 if (mmc_write(ptn , 512, (unsigned int *)out)) {
935 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700936 return;
937 }
938 fastboot_okay("");
939}
940
941
Ajay Dudani5c761132011-04-07 20:19:04 -0700942void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700943{
944 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700945 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700946 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700947
Greg Grisco6e754772011-06-23 12:19:39 -0700948 if (!strcmp(arg, "partition"))
949 {
950 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -0700951 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -0700952 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700953 return;
954 }
955 }
Greg Grisco6e754772011-06-23 12:19:39 -0700956 else
957 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700958 index = partition_get_index(arg);
959 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700960 if(ptn == 0) {
961 fastboot_fail("partition table doesn't exist");
962 return;
963 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700964
Greg Grisco6e754772011-06-23 12:19:39 -0700965 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
966 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
967 fastboot_fail("image is not a boot image");
968 return;
969 }
970 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700971
Kinson Chikf1a43512011-07-14 11:28:39 -0700972 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700973 if (ROUND_TO_PAGE(sz,511) > size) {
974 fastboot_fail("size too large");
975 return;
976 }
977 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
978 fastboot_fail("flash write failure");
979 return;
980 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700981 }
982 fastboot_okay("");
983 return;
984}
985
Ajay Dudani5c761132011-04-07 20:19:04 -0700986void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
987{
988 unsigned int chunk;
989 unsigned int chunk_data_sz;
990 sparse_header_t *sparse_header;
991 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700992 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700993 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530994 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700995 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700996
Kinson Chikf1a43512011-07-14 11:28:39 -0700997 index = partition_get_index(arg);
998 ptn = partition_get_offset(index);
999 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001000 fastboot_fail("partition table doesn't exist");
1001 return;
1002 }
1003
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301004 size = partition_get_size(index);
1005 if (ROUND_TO_PAGE(sz,511) > size) {
1006 fastboot_fail("size too large");
1007 return;
1008 }
1009
Ajay Dudani5c761132011-04-07 20:19:04 -07001010 /* Read and skip over sparse image header */
1011 sparse_header = (sparse_header_t *) data;
1012 data += sparse_header->file_hdr_sz;
1013 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1014 {
1015 /* Skip the remaining bytes in a header that is longer than
1016 * we expected.
1017 */
1018 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1019 }
1020
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001021 dprintf (SPEW, "=== Sparse Image Header ===\n");
1022 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1023 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1024 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1025 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1026 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1027 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1028 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1029 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001030
1031 /* Start processing chunks */
1032 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1033 {
1034 /* Read and skip over chunk header */
1035 chunk_header = (chunk_header_t *) data;
1036 data += sizeof(chunk_header_t);
1037
1038 dprintf (SPEW, "=== Chunk Header ===\n");
1039 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1040 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1041 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1042
1043 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1044 {
1045 /* Skip the remaining bytes in a header that is longer than
1046 * we expected.
1047 */
1048 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1049 }
1050
1051 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1052 switch (chunk_header->chunk_type)
1053 {
1054 case CHUNK_TYPE_RAW:
1055 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1056 chunk_data_sz))
1057 {
1058 fastboot_fail("Bogus chunk size for chunk type Raw");
1059 return;
1060 }
1061
Ajay Dudaniab18f022011-05-12 14:39:22 -07001062 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1063 chunk_data_sz,
1064 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001065 {
1066 fastboot_fail("flash write failure");
1067 return;
1068 }
1069 total_blocks += chunk_header->chunk_sz;
1070 data += chunk_data_sz;
1071 break;
1072
1073 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001074 total_blocks += chunk_header->chunk_sz;
1075 break;
1076
Ajay Dudani5c761132011-04-07 20:19:04 -07001077 case CHUNK_TYPE_CRC:
1078 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1079 {
1080 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1081 return;
1082 }
1083 total_blocks += chunk_header->chunk_sz;
1084 data += chunk_data_sz;
1085 break;
1086
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001087 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001088 fastboot_fail("Unknown chunk type");
1089 return;
1090 }
1091 }
1092
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001093 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1094 total_blocks, sparse_header->total_blks);
1095
1096 if(total_blocks != sparse_header->total_blks)
1097 {
1098 fastboot_fail("sparse image write failure");
1099 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001100
1101 fastboot_okay("");
1102 return;
1103}
1104
1105void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1106{
1107 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001108 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1109 unsigned int *magic_number = (unsigned int *) data;
1110 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001111
Neeti Desai127b9e02012-03-20 16:11:23 -07001112 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1113 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001114 {
1115#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001116 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001117#endif
Greg Griscod6250552011-06-29 14:40:23 -07001118 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001119 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1120 return;
1121 }
1122 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001123 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1124 magic_number[1] == ENCRYPT_MAGIC_1)
1125 {
1126#ifdef SSD_ENABLE
1127 ret = encrypt_scm((uint32 **) &data, &sz);
1128#endif
1129 if (ret != 0) {
1130 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1131 return;
1132 }
1133 }
1134
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001135 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001136 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1137 cmd_flash_mmc_img(arg, data, sz);
1138 else
1139 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001140 return;
1141}
1142
Dima Zavin214cc642009-01-26 11:16:21 -08001143void cmd_flash(const char *arg, void *data, unsigned sz)
1144{
1145 struct ptentry *ptn;
1146 struct ptable *ptable;
1147 unsigned extra = 0;
1148
1149 ptable = flash_get_ptable();
1150 if (ptable == NULL) {
1151 fastboot_fail("partition table doesn't exist");
1152 return;
1153 }
1154
1155 ptn = ptable_find(ptable, arg);
1156 if (ptn == NULL) {
1157 fastboot_fail("unknown partition name");
1158 return;
1159 }
1160
1161 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1162 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1163 fastboot_fail("image is not a boot image");
1164 return;
1165 }
1166 }
1167
Amol Jadi5c61a952012-05-04 17:05:35 -07001168 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001169 || !strcmp(ptn->name, "userdata")
1170 || !strcmp(ptn->name, "persist")
1171 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301172 if (flash_ecc_bch_enabled())
1173 /* Spare data bytes for 8 bit ECC increased by 4 */
1174 extra = ((page_size >> 9) * 20);
1175 else
1176 extra = ((page_size >> 9) * 16);
1177 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001178 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001179
1180 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1181 if (flash_write(ptn, extra, data, sz)) {
1182 fastboot_fail("flash write failure");
1183 return;
1184 }
1185 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1186 fastboot_okay("");
1187}
1188
1189void cmd_continue(const char *arg, void *data, unsigned sz)
1190{
1191 fastboot_okay("");
1192 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001193 if (target_is_emmc_boot())
1194 {
1195 boot_linux_from_mmc();
1196 }
1197 else
1198 {
1199 boot_linux_from_flash();
1200 }
Dima Zavin214cc642009-01-26 11:16:21 -08001201}
1202
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001203void cmd_reboot(const char *arg, void *data, unsigned sz)
1204{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001205 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001206 fastboot_okay("");
1207 reboot_device(0);
1208}
1209
1210void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1211{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001212 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001213 fastboot_okay("");
1214 reboot_device(FASTBOOT_MODE);
1215}
1216
Shashank Mittal162244e2011-08-08 19:01:25 -07001217void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1218{
1219 if(!device.is_unlocked)
1220 {
1221 device.is_unlocked = 1;
1222 write_device_info(&device);
1223 }
1224 fastboot_okay("");
1225}
1226
Shashank Mittala0032282011-08-26 14:50:11 -07001227void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1228{
1229 char response[64];
1230 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1231 fastboot_info(response);
1232 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1233 fastboot_info(response);
1234 fastboot_okay("");
1235}
1236
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001237void splash_screen ()
1238{
1239 struct ptentry *ptn;
1240 struct ptable *ptable;
1241 struct fbcon_config *fb_display = NULL;
1242
1243 if (!target_is_emmc_boot())
1244 {
1245 ptable = flash_get_ptable();
1246 if (ptable == NULL) {
1247 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001248 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001249 }
1250
1251 ptn = ptable_find(ptable, "splash");
1252 if (ptn == NULL) {
1253 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1254 } else {
1255 fb_display = fbcon_display();
1256 if (fb_display) {
1257 if (flash_read(ptn, 0, fb_display->base,
1258 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1259 fbcon_clear();
1260 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1261 }
1262 }
1263 }
1264 }
1265}
1266
Brian Swetland9c4c0752009-01-25 16:23:50 -08001267void aboot_init(const struct app_descriptor *app)
1268{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001269 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001270 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001271 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001272
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001273 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001274 if (target_is_emmc_boot())
1275 {
1276 page_size = 2048;
1277 page_mask = page_size - 1;
1278 }
1279 else
1280 {
1281 page_size = flash_page_size();
1282 page_mask = page_size - 1;
1283 }
1284
Shashank Mittal162244e2011-08-08 19:01:25 -07001285 if(target_use_signed_kernel())
1286 {
1287 read_device_info(&device);
1288
Shashank Mittal162244e2011-08-08 19:01:25 -07001289 }
1290
Greg Griscod6250552011-06-29 14:40:23 -07001291 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001292 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001293 surf_udc_device.serialno = sn_buf;
1294
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001295 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001296 if (keys_get_state(KEY_HOME) != 0)
1297 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001298 if (keys_get_state(KEY_VOLUMEUP) != 0)
1299 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001300 if(!boot_into_recovery)
1301 {
1302 if (keys_get_state(KEY_BACK) != 0)
1303 goto fastboot;
1304 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1305 goto fastboot;
1306 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001307
1308 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001309 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001310 goto fastboot;
1311 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001312
Ajay Dudani77421292010-10-27 19:34:06 -07001313 reboot_mode = check_reboot_mode();
1314 if (reboot_mode == RECOVERY_MODE) {
1315 boot_into_recovery = 1;
1316 } else if(reboot_mode == FASTBOOT_MODE) {
1317 goto fastboot;
1318 }
1319
Shashank Mittal23b8f422010-04-16 19:27:21 -07001320 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001321 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001322 if(emmc_recovery_init())
1323 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001324 if(target_use_signed_kernel())
1325 {
1326 if((device.is_unlocked) || (device.is_tampered))
1327 {
1328 #ifdef TZ_TAMPER_FUSE
1329 set_tamper_fuse_cmd();
1330 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301331 #if USE_PCOM_SECBOOT
1332 set_tamper_flag(device.is_tampered);
1333 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001334 }
1335 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001336 boot_linux_from_mmc();
1337 }
1338 else
1339 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001340 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301341#if USE_PCOM_SECBOOT
1342 if((device.is_unlocked) || (device.is_tampered))
1343 set_tamper_flag(device.is_tampered);
1344#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001345 boot_linux_from_flash();
1346 }
Dima Zavinb4283602009-01-26 16:36:57 -08001347 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1348 "to fastboot mode.\n");
1349
1350fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001351
Shashank Mittal162244e2011-08-08 19:01:25 -07001352 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001353
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001354 if(!usb_init)
1355 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001356
1357 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001358
Shashank Mittal23b8f422010-04-16 19:27:21 -07001359 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001360 {
1361 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001362 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001363 }
1364 else
1365 {
1366 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001367 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001368 }
1369
1370 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001371 fastboot_register("reboot", cmd_reboot);
1372 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001373 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001374 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001375 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001376 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001377 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001378 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001379 sz = target_get_max_flash_size();
1380 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001381 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001382}
1383
1384APP_START(aboot)
1385 .init = aboot_init,
1386APP_END
1387