blob: fabccb1726fedc2d3a1c8e917490a3919bd154c4 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Ajay Dudanid04110c2011-01-17 23:55:07 -08005 * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Code Aurora nor
15 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
36#include <dev/udc.h>
37#include <string.h>
38#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Dima Zavin214cc642009-01-26 11:16:21 -080051
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080053#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080054#include "bootimg.h"
55#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070056#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070057#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070058#include "devinfo.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080059
Shashank Mittal162244e2011-08-08 19:01:25 -070060#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070061
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070062#define EXPAND(NAME) #NAME
63#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080064#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070065
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080066#ifdef MEMBASE
67#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
68#else
David Ng183a7422009-12-07 14:55:21 -080069#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080070#endif
71
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080072#define RECOVERY_MODE 0x77665502
73#define FASTBOOT_MODE 0x77665500
74
David Ng183a7422009-12-07 14:55:21 -080075static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080076static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070077static const char *battchg_pause = " androidboot.battchg_pause=true";
Shashank Mittalcd98d472011-08-02 14:29:24 -070078static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080079
Ajay Dudani6cff85e2011-02-04 16:02:16 -080080static const char *baseband_apq = " androidboot.baseband=apq";
81static const char *baseband_msm = " androidboot.baseband=msm";
82static const char *baseband_csfb = " androidboot.baseband=csfb";
83static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080084
Shashank Mittalcd98d472011-08-02 14:29:24 -070085/* Assuming unauthorized kernel image by default */
86static int auth_kernel_img = 0;
87
Shashank Mittal162244e2011-08-08 19:01:25 -070088static device_info device = {DEVICE_MAGIC, 0, 0};
89
Brian Swetland9c4c0752009-01-25 16:23:50 -080090static struct udc_device surf_udc_device = {
91 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080092 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080093 .version_id = 0x0100,
94 .manufacturer = "Google",
95 .product = "Android",
96};
97
Dima Zavin42168f22009-01-30 11:52:22 -080098struct atag_ptbl_entry
99{
100 char name[16];
101 unsigned offset;
102 unsigned size;
103 unsigned flags;
104};
105
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800106char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700107
Greg Griscod2471ef2011-07-14 13:00:42 -0700108extern int emmc_recovery_init(void);
109
Kinson Chik0b1c8162011-08-31 16:31:57 -0700110#if NO_KEYPAD_DRIVER
111extern int fastboot_trigger(void);
112#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700113
Dima Zavin42168f22009-01-30 11:52:22 -0800114static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
115{
116 struct atag_ptbl_entry atag_ptn;
117
Greg Griscod6250552011-06-29 14:40:23 -0700118 if (ptn->type == TYPE_MODEM_PARTITION) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800119 return;
Greg Griscod6250552011-06-29 14:40:23 -0700120 }
Dima Zavin42168f22009-01-30 11:52:22 -0800121 memcpy(atag_ptn.name, ptn->name, 16);
122 atag_ptn.name[15] = '\0';
123 atag_ptn.offset = ptn->start;
124 atag_ptn.size = ptn->length;
125 atag_ptn.flags = ptn->flags;
126 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
127 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
128}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700130void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800131 const char *cmdline, unsigned machtype,
132 void *ramdisk, unsigned ramdisk_size)
133{
134 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800135 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800136 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800137 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800138 int cmdline_len = 0;
139 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700140 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800141
142 /* CORE */
143 *ptr++ = 2;
144 *ptr++ = 0x54410001;
145
146 if (ramdisk_size) {
147 *ptr++ = 4;
148 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800149 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800150 *ptr++ = ramdisk_size;
151 }
152
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800153 ptr = target_atag_mem(ptr);
154
David Ng183a7422009-12-07 14:55:21 -0800155 if (!target_is_emmc_boot()) {
156 /* Skip NAND partition ATAGS for eMMC boot */
157 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
158 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800159 for(i=0; i < ptable->count; i++) {
160 struct ptentry *ptn;
161 ptn = ptable_get(ptable, i);
162 if (ptn->type == TYPE_APPS_PARTITION)
163 pcount++;
164 }
165 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800166 sizeof(unsigned)));
167 *ptr++ = 0x4d534d70;
168 for (i = 0; i < ptable->count; ++i)
169 ptentry_to_tag(&ptr, ptable_get(ptable, i));
170 }
Dima Zavin42168f22009-01-30 11:52:22 -0800171 }
172
Brian Swetland9c4c0752009-01-25 16:23:50 -0800173 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800174 cmdline_len = strlen(cmdline);
175 have_cmdline = 1;
176 }
177 if (target_is_emmc_boot()) {
178 cmdline_len += strlen(emmc_cmdline);
179 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800180
181 cmdline_len += strlen(usb_sn_cmdline);
182 cmdline_len += strlen(sn_buf);
183
David Ngf773dde2010-07-26 19:55:08 -0700184 if (target_pause_for_battery_charge()) {
185 pause_at_bootup = 1;
186 cmdline_len += strlen(battchg_pause);
187 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800188
Shashank Mittalcd98d472011-08-02 14:29:24 -0700189 if(target_use_signed_kernel() && auth_kernel_img) {
190 cmdline_len += strlen(auth_kernel);
191 }
192
Ajay Dudanid04110c2011-01-17 23:55:07 -0800193 /* Determine correct androidboot.baseband to use */
194 switch(target_baseband())
195 {
196 case BASEBAND_APQ:
197 cmdline_len += strlen(baseband_apq);
198 break;
199
200 case BASEBAND_MSM:
201 cmdline_len += strlen(baseband_msm);
202 break;
203
204 case BASEBAND_CSFB:
205 cmdline_len += strlen(baseband_csfb);
206 break;
207
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800208 case BASEBAND_SVLTE2A:
209 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800210 break;
211 }
212
David Ng183a7422009-12-07 14:55:21 -0800213 if (cmdline_len > 0) {
214 const char *src;
215 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800216 unsigned n;
217 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800218 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800219 *ptr++ = (n / 4) + 2;
220 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800221 dst = (char *)ptr;
222 if (have_cmdline) {
223 src = cmdline;
224 while ((*dst++ = *src++));
225 }
226 if (target_is_emmc_boot()) {
227 src = emmc_cmdline;
228 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700229 have_cmdline = 1;
230 while ((*dst++ = *src++));
231 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800232
233 src = usb_sn_cmdline;
234 if (have_cmdline) --dst;
235 have_cmdline = 1;
236 while ((*dst++ = *src++));
237 src = sn_buf;
238 if (have_cmdline) --dst;
239 have_cmdline = 1;
240 while ((*dst++ = *src++));
241
David Ngf773dde2010-07-26 19:55:08 -0700242 if (pause_at_bootup) {
243 src = battchg_pause;
244 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800245 while ((*dst++ = *src++));
246 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800247
Shashank Mittalcd98d472011-08-02 14:29:24 -0700248 if(target_use_signed_kernel() && auth_kernel_img) {
249 src = auth_kernel;
250 if (have_cmdline) --dst;
251 while ((*dst++ = *src++));
252 }
253
Ajay Dudanid04110c2011-01-17 23:55:07 -0800254 switch(target_baseband())
255 {
256 case BASEBAND_APQ:
257 src = baseband_apq;
258 if (have_cmdline) --dst;
259 while ((*dst++ = *src++));
260 break;
261
262 case BASEBAND_MSM:
263 src = baseband_msm;
264 if (have_cmdline) --dst;
265 while ((*dst++ = *src++));
266 break;
267
268 case BASEBAND_CSFB:
269 src = baseband_csfb;
270 if (have_cmdline) --dst;
271 while ((*dst++ = *src++));
272 break;
273
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800274 case BASEBAND_SVLTE2A:
275 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800276 if (have_cmdline) --dst;
277 while ((*dst++ = *src++));
278 break;
279 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800280 ptr += (n / 4);
281 }
282
283 /* END */
284 *ptr++ = 0;
285 *ptr++ = 0;
286
287 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
288 kernel, ramdisk, ramdisk_size);
289 if (cmdline)
290 dprintf(INFO, "cmdline: %s\n", cmdline);
291
292 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700293 /* do any platform specific cleanup before kernel entry */
294 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800295 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800296 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800297 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800298}
299
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800300unsigned page_size = 0;
301unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800302
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800303#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800304
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800305static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800306
Shashank Mittal23b8f422010-04-16 19:27:21 -0700307int boot_linux_from_mmc(void)
308{
309 struct boot_img_hdr *hdr = (void*) buf;
310 struct boot_img_hdr *uhdr;
311 unsigned offset = 0;
312 unsigned long long ptn = 0;
313 unsigned n = 0;
314 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700315 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700316
Shashank Mittalcd98d472011-08-02 14:29:24 -0700317 unsigned char *image_addr = 0;
318 unsigned kernel_actual;
319 unsigned ramdisk_actual;
320 unsigned imagesize_actual;
321
Shashank Mittal23b8f422010-04-16 19:27:21 -0700322 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
323 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
324 dprintf(INFO, "Unified boot method!\n");
325 hdr = uhdr;
326 goto unified_boot;
327 }
Greg Griscod6250552011-06-29 14:40:23 -0700328 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700329 index = partition_get_index("boot");
330 ptn = partition_get_offset(index);
331 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700332 dprintf(CRITICAL, "ERROR: No boot partition found\n");
333 return -1;
334 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700335 }
336 else {
337 index = partition_get_index("recovery");
338 ptn = partition_get_offset(index);
339 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700340 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
341 return -1;
342 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700343 }
344
Greg Griscod6250552011-06-29 14:40:23 -0700345 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700346 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
347 return -1;
348 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700349
350 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700351 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700352 return -1;
353 }
354
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700355 if (hdr->page_size && (hdr->page_size != page_size)) {
356 page_size = hdr->page_size;
357 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700358 }
359
Shashank Mittalcd98d472011-08-02 14:29:24 -0700360 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700361 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800362 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700363 image_addr = (unsigned char *)target_get_scratch_address();
364 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
365 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
366 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
367
368 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700369
370 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700371 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700372
Shashank Mittalcd98d472011-08-02 14:29:24 -0700373 /* Read image without signature */
374 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
375 {
376 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
377 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800378 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700379
380 offset = imagesize_actual;
381 /* Read signature */
382 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
383 {
384 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
385 }
386 else
387 {
388 auth_kernel_img = image_verify((unsigned char *)image_addr,
389 (unsigned char *)(image_addr + imagesize_actual),
390 imagesize_actual,
391 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700392
393 if(auth_kernel_img)
394 {
395 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700396 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700397 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700398 }
399
400 /* Move kernel and ramdisk to correct address */
401 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
402 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700403
404 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700405 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700406 {
407 write_device_info_mmc(&device);
408 #ifdef TZ_TAMPER_FUSE
409 set_tamper_fuse_cmd();
410 #endif
411 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700412 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700413 else
414 {
415 offset += page_size;
416
417 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
418 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
419 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
420 return -1;
421 }
422 offset += n;
423
424 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
425 if(n != 0)
426 {
427 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
428 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
429 return -1;
430 }
431 }
432 offset += n;
433 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700434
435unified_boot:
436 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
437 hdr->kernel_size);
438 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
439 hdr->ramdisk_size);
440
441 if(hdr->cmdline[0]) {
442 cmdline = (char*) hdr->cmdline;
443 } else {
444 cmdline = DEFAULT_CMDLINE;
445 }
446 dprintf(INFO, "cmdline = '%s'\n", cmdline);
447
448 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700449 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700450 (const char *)cmdline, board_machtype(),
451 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
452
453 return 0;
454}
455
Dima Zavin214cc642009-01-26 11:16:21 -0800456int boot_linux_from_flash(void)
457{
458 struct boot_img_hdr *hdr = (void*) buf;
459 unsigned n;
460 struct ptentry *ptn;
461 struct ptable *ptable;
462 unsigned offset = 0;
463 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800464
Shashank Mittalcd98d472011-08-02 14:29:24 -0700465 unsigned char *image_addr = 0;
466 unsigned kernel_actual;
467 unsigned ramdisk_actual;
468 unsigned imagesize_actual;
469
David Ng183a7422009-12-07 14:55:21 -0800470 if (target_is_emmc_boot()) {
471 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
472 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
473 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
474 return -1;
475 }
476 goto continue_boot;
477 }
478
Dima Zavin214cc642009-01-26 11:16:21 -0800479 ptable = flash_get_ptable();
480 if (ptable == NULL) {
481 dprintf(CRITICAL, "ERROR: Partition table not found\n");
482 return -1;
483 }
484
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800485 if(!boot_into_recovery)
486 {
487 ptn = ptable_find(ptable, "boot");
488 if (ptn == NULL) {
489 dprintf(CRITICAL, "ERROR: No boot partition found\n");
490 return -1;
491 }
492 }
493 else
494 {
495 ptn = ptable_find(ptable, "recovery");
496 if (ptn == NULL) {
497 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
498 return -1;
499 }
Dima Zavin214cc642009-01-26 11:16:21 -0800500 }
501
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800502 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800503 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
504 return -1;
505 }
Dima Zavin214cc642009-01-26 11:16:21 -0800506
507 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700508 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800509 return -1;
510 }
511
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800512 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700513 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 -0800514 return -1;
515 }
516
Shashank Mittalcd98d472011-08-02 14:29:24 -0700517 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700518 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700519 {
520 image_addr = (unsigned char *)target_get_scratch_address();
521 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
522 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
523 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800524
Shashank Mittalcd98d472011-08-02 14:29:24 -0700525 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700526
527 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700528 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700529
Shashank Mittalcd98d472011-08-02 14:29:24 -0700530 /* Read image without signature */
531 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
532 {
533 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
534 return -1;
535 }
Dima Zavin214cc642009-01-26 11:16:21 -0800536
Shashank Mittalcd98d472011-08-02 14:29:24 -0700537 offset = imagesize_actual;
538 /* Read signature */
539 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
540 {
541 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
542 }
543 else
544 {
545
546 /* Verify signature */
547 auth_kernel_img = image_verify((unsigned char *)image_addr,
548 (unsigned char *)(image_addr + imagesize_actual),
549 imagesize_actual,
550 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700551
552 if(auth_kernel_img)
553 {
554 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700555 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700556 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700557 }
558
559 /* Move kernel and ramdisk to correct address */
560 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
561 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700562
563 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700564 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700565 {
566 write_device_info_flash(&device);
567 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700568 }
569 else
570 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700571 offset = page_size;
572
Shashank Mittalcd98d472011-08-02 14:29:24 -0700573 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
574 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
575 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
576 return -1;
577 }
578 offset += n;
579
580 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
581 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
582 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
583 return -1;
584 }
585 offset += n;
586 }
David Ng183a7422009-12-07 14:55:21 -0800587continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800588 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
589 hdr->kernel_size);
590 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
591 hdr->ramdisk_size);
592
593 if(hdr->cmdline[0]) {
594 cmdline = (char*) hdr->cmdline;
595 } else {
596 cmdline = DEFAULT_CMDLINE;
597 }
598 dprintf(INFO, "cmdline = '%s'\n", cmdline);
599
600 /* TODO: create/pass atags to kernel */
601
602 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700603 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800604 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800605 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
606
607 return 0;
608}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800609
Shashank Mittal162244e2011-08-08 19:01:25 -0700610unsigned char info_buf[4096];
611void write_device_info_mmc(device_info *dev)
612{
613 struct device_info *info = (void*) info_buf;
614 unsigned long long ptn = 0;
615 unsigned long long size;
616 int index = INVALID_PTN;
617
618 index = partition_get_index("aboot");
619 ptn = partition_get_offset(index);
620 if(ptn == 0)
621 {
622 return;
623 }
624
625 size = partition_get_size(index);
626
627 memcpy(info, dev, sizeof(device_info));
628
629 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
630 {
631 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
632 return;
633 }
634}
635
636void read_device_info_mmc(device_info *dev)
637{
638 struct device_info *info = (void*) info_buf;
639 unsigned long long ptn = 0;
640 unsigned long long size;
641 int index = INVALID_PTN;
642
643 index = partition_get_index("aboot");
644 ptn = partition_get_offset(index);
645 if(ptn == 0)
646 {
647 return;
648 }
649
650 size = partition_get_size(index);
651
652 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
653 {
654 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
655 return;
656 }
657
658 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
659 {
660 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
661 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700662 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700663
664 write_device_info_mmc(info);
665 }
666 memcpy(dev, info, sizeof(device_info));
667}
668
669void write_device_info_flash(device_info *dev)
670{
671 struct device_info *info = (void *) info_buf;
672 struct ptentry *ptn;
673 struct ptable *ptable;
674
675 ptable = flash_get_ptable();
676 if (ptable == NULL)
677 {
678 dprintf(CRITICAL, "ERROR: Partition table not found\n");
679 return;
680 }
681
682 ptn = ptable_find(ptable, "devinfo");
683 if (ptn == NULL)
684 {
685 dprintf(CRITICAL, "ERROR: No boot partition found\n");
686 return;
687 }
688
689 memcpy(info, dev, sizeof(device_info));
690
691 if (flash_write(ptn, 0, (void *)info_buf, page_size))
692 {
693 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
694 return;
695 }
696}
697
698void read_device_info_flash(device_info *dev)
699{
700 struct device_info *info = (void*) info_buf;
701 struct ptentry *ptn;
702 struct ptable *ptable;
703
704 ptable = flash_get_ptable();
705 if (ptable == NULL)
706 {
707 dprintf(CRITICAL, "ERROR: Partition table not found\n");
708 return;
709 }
710
711 ptn = ptable_find(ptable, "devinfo");
712 if (ptn == NULL)
713 {
714 dprintf(CRITICAL, "ERROR: No boot partition found\n");
715 return;
716 }
717
718 if (flash_read(ptn, 0, (void *)info_buf, page_size))
719 {
720 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
721 return;
722 }
723
724 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
725 {
726 while(1);
727 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
728 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700729 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700730 write_device_info_flash(info);
731 }
732 memcpy(dev, info, sizeof(device_info));
733}
734
735void write_device_info(device_info *dev)
736{
737 if(target_is_emmc_boot())
738 {
739 write_device_info_mmc(dev);
740 }
741 else
742 {
743 write_device_info_flash(dev);
744 }
745}
746
747void read_device_info(device_info *dev)
748{
749 if(target_is_emmc_boot())
750 {
751 read_device_info_mmc(dev);
752 }
753 else
754 {
755 read_device_info_flash(dev);
756 }
757}
758
759void reset_device_info()
760{
761 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700762 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700763 write_device_info(&device);
764}
765
766void set_device_root()
767{
768 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700769 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700770 write_device_info(&device);
771}
772
Brian Swetland9c4c0752009-01-25 16:23:50 -0800773void cmd_boot(const char *arg, void *data, unsigned sz)
774{
775 unsigned kernel_actual;
776 unsigned ramdisk_actual;
777 static struct boot_img_hdr hdr;
778 char *ptr = ((char*) data);
779
780 if (sz < sizeof(hdr)) {
781 fastboot_fail("invalid bootimage header");
782 return;
783 }
784
785 memcpy(&hdr, data, sizeof(hdr));
786
787 /* ensure commandline is terminated */
788 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
789
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700790 if(target_is_emmc_boot() && hdr.page_size) {
791 page_size = hdr.page_size;
792 page_mask = page_size - 1;
793 }
794
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800795 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
796 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800797
Shashank Mittal1f0e2662011-09-01 15:06:00 -0700798 /* sz should have atleast raw boot image */
799 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800800 fastboot_fail("incomplete bootimage");
801 return;
802 }
803
Ajay Dudanie28a6072011-07-01 13:59:46 -0700804 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
805 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800806
807 fastboot_okay("");
808 udc_stop();
809
Amol Jadie67872e2011-06-27 14:14:11 -0700810 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800811 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700812 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800813}
814
Dima Zavin214cc642009-01-26 11:16:21 -0800815void cmd_erase(const char *arg, void *data, unsigned sz)
816{
817 struct ptentry *ptn;
818 struct ptable *ptable;
819
820 ptable = flash_get_ptable();
821 if (ptable == NULL) {
822 fastboot_fail("partition table doesn't exist");
823 return;
824 }
825
826 ptn = ptable_find(ptable, arg);
827 if (ptn == NULL) {
828 fastboot_fail("unknown partition name");
829 return;
830 }
831
832 if (flash_erase(ptn)) {
833 fastboot_fail("failed to erase partition");
834 return;
835 }
836 fastboot_okay("");
837}
838
Bikas Gurungd48bd242010-09-04 19:54:32 -0700839
840void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
841{
842 unsigned long long ptn = 0;
843 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -0700844 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700845
Kinson Chikf1a43512011-07-14 11:28:39 -0700846 index = partition_get_index(arg);
847 ptn = partition_get_offset(index);
848 if(ptn == 0) {
Bikas Gurungd48bd242010-09-04 19:54:32 -0700849 fastboot_fail("partition table doesn't exist");
850 return;
851 }
852
Bikas Gurungd48bd242010-09-04 19:54:32 -0700853 /* Simple inefficient version of erase. Just writing
854 0 in first block */
855 if (mmc_write(ptn , 512, (unsigned int *)out)) {
856 fastboot_fail("failed to erase partition");
857 return;
858 }
859 fastboot_okay("");
860}
861
862
Ajay Dudani5c761132011-04-07 20:19:04 -0700863void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700864{
865 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700866 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700867 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700868
Greg Grisco6e754772011-06-23 12:19:39 -0700869 if (!strcmp(arg, "partition"))
870 {
871 dprintf(INFO, "Attempt to write partition image.\n");
872 if (mmc_write_partition(sz, (unsigned char *) data)) {
873 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700874 return;
875 }
876 }
Greg Grisco6e754772011-06-23 12:19:39 -0700877 else
878 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700879 index = partition_get_index(arg);
880 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700881 if(ptn == 0) {
882 fastboot_fail("partition table doesn't exist");
883 return;
884 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700885
Greg Grisco6e754772011-06-23 12:19:39 -0700886 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
887 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
888 fastboot_fail("image is not a boot image");
889 return;
890 }
891 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700892
Kinson Chikf1a43512011-07-14 11:28:39 -0700893 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700894 if (ROUND_TO_PAGE(sz,511) > size) {
895 fastboot_fail("size too large");
896 return;
897 }
898 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
899 fastboot_fail("flash write failure");
900 return;
901 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700902 }
903 fastboot_okay("");
904 return;
905}
906
Ajay Dudani5c761132011-04-07 20:19:04 -0700907void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
908{
909 unsigned int chunk;
910 unsigned int chunk_data_sz;
911 sparse_header_t *sparse_header;
912 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700913 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700914 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700915 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700916
Kinson Chikf1a43512011-07-14 11:28:39 -0700917 index = partition_get_index(arg);
918 ptn = partition_get_offset(index);
919 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700920 fastboot_fail("partition table doesn't exist");
921 return;
922 }
923
924 /* Read and skip over sparse image header */
925 sparse_header = (sparse_header_t *) data;
926 data += sparse_header->file_hdr_sz;
927 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
928 {
929 /* Skip the remaining bytes in a header that is longer than
930 * we expected.
931 */
932 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
933 }
934
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700935 dprintf (SPEW, "=== Sparse Image Header ===\n");
936 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
937 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
938 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
939 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
940 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
941 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
942 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
943 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700944
945 /* Start processing chunks */
946 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
947 {
948 /* Read and skip over chunk header */
949 chunk_header = (chunk_header_t *) data;
950 data += sizeof(chunk_header_t);
951
952 dprintf (SPEW, "=== Chunk Header ===\n");
953 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
954 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
955 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
956
957 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
958 {
959 /* Skip the remaining bytes in a header that is longer than
960 * we expected.
961 */
962 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
963 }
964
965 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
966 switch (chunk_header->chunk_type)
967 {
968 case CHUNK_TYPE_RAW:
969 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
970 chunk_data_sz))
971 {
972 fastboot_fail("Bogus chunk size for chunk type Raw");
973 return;
974 }
975
Ajay Dudaniab18f022011-05-12 14:39:22 -0700976 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
977 chunk_data_sz,
978 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700979 {
980 fastboot_fail("flash write failure");
981 return;
982 }
983 total_blocks += chunk_header->chunk_sz;
984 data += chunk_data_sz;
985 break;
986
987 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700988 total_blocks += chunk_header->chunk_sz;
989 break;
990
Ajay Dudani5c761132011-04-07 20:19:04 -0700991 case CHUNK_TYPE_CRC:
992 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
993 {
994 fastboot_fail("Bogus chunk size for chunk type Dont Care");
995 return;
996 }
997 total_blocks += chunk_header->chunk_sz;
998 data += chunk_data_sz;
999 break;
1000
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001001 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001002 fastboot_fail("Unknown chunk type");
1003 return;
1004 }
1005 }
1006
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001007 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1008 total_blocks, sparse_header->total_blks);
1009
1010 if(total_blocks != sparse_header->total_blks)
1011 {
1012 fastboot_fail("sparse image write failure");
1013 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001014
1015 fastboot_okay("");
1016 return;
1017}
1018
1019void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1020{
1021 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001022 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1023 unsigned int *magic_number = (unsigned int *) data;
1024 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001025
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001026 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
1027 magic_number[1] == SSD_HEADER_MAGIC_1)
1028 {
1029#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -07001030 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001031#endif
Greg Griscod6250552011-06-29 14:40:23 -07001032 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001033 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1034 return;
1035 }
1036 }
1037 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001038 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1039 cmd_flash_mmc_img(arg, data, sz);
1040 else
1041 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001042 return;
1043}
1044
Dima Zavin214cc642009-01-26 11:16:21 -08001045void cmd_flash(const char *arg, void *data, unsigned sz)
1046{
1047 struct ptentry *ptn;
1048 struct ptable *ptable;
1049 unsigned extra = 0;
1050
1051 ptable = flash_get_ptable();
1052 if (ptable == NULL) {
1053 fastboot_fail("partition table doesn't exist");
1054 return;
1055 }
1056
1057 ptn = ptable_find(ptable, arg);
1058 if (ptn == NULL) {
1059 fastboot_fail("unknown partition name");
1060 return;
1061 }
1062
1063 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1064 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1065 fastboot_fail("image is not a boot image");
1066 return;
1067 }
1068 }
1069
Chandan Uddarajud6d45042010-02-24 21:12:45 -08001070 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301071 || !strcmp(ptn->name, "persist")) {
1072 if (flash_ecc_bch_enabled())
1073 /* Spare data bytes for 8 bit ECC increased by 4 */
1074 extra = ((page_size >> 9) * 20);
1075 else
1076 extra = ((page_size >> 9) * 16);
1077 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001078 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001079
1080 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1081 if (flash_write(ptn, extra, data, sz)) {
1082 fastboot_fail("flash write failure");
1083 return;
1084 }
1085 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1086 fastboot_okay("");
1087}
1088
1089void cmd_continue(const char *arg, void *data, unsigned sz)
1090{
1091 fastboot_okay("");
1092 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001093 if (target_is_emmc_boot())
1094 {
1095 boot_linux_from_mmc();
1096 }
1097 else
1098 {
1099 boot_linux_from_flash();
1100 }
Dima Zavin214cc642009-01-26 11:16:21 -08001101}
1102
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001103void cmd_reboot(const char *arg, void *data, unsigned sz)
1104{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001105 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001106 fastboot_okay("");
1107 reboot_device(0);
1108}
1109
1110void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1111{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001112 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001113 fastboot_okay("");
1114 reboot_device(FASTBOOT_MODE);
1115}
1116
Shashank Mittal162244e2011-08-08 19:01:25 -07001117void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1118{
1119 if(!device.is_unlocked)
1120 {
1121 device.is_unlocked = 1;
1122 write_device_info(&device);
1123 }
1124 fastboot_okay("");
1125}
1126
Shashank Mittala0032282011-08-26 14:50:11 -07001127void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1128{
1129 char response[64];
1130 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1131 fastboot_info(response);
1132 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1133 fastboot_info(response);
1134 fastboot_okay("");
1135}
1136
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001137void splash_screen ()
1138{
1139 struct ptentry *ptn;
1140 struct ptable *ptable;
1141 struct fbcon_config *fb_display = NULL;
1142
1143 if (!target_is_emmc_boot())
1144 {
1145 ptable = flash_get_ptable();
1146 if (ptable == NULL) {
1147 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001148 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001149 }
1150
1151 ptn = ptable_find(ptable, "splash");
1152 if (ptn == NULL) {
1153 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1154 } else {
1155 fb_display = fbcon_display();
1156 if (fb_display) {
1157 if (flash_read(ptn, 0, fb_display->base,
1158 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1159 fbcon_clear();
1160 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1161 }
1162 }
1163 }
1164 }
1165}
1166
Brian Swetland9c4c0752009-01-25 16:23:50 -08001167void aboot_init(const struct app_descriptor *app)
1168{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001169 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001170 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001171 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001172
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001173 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001174 if (target_is_emmc_boot())
1175 {
1176 page_size = 2048;
1177 page_mask = page_size - 1;
1178 }
1179 else
1180 {
1181 page_size = flash_page_size();
1182 page_mask = page_size - 1;
1183 }
1184
Shashank Mittal162244e2011-08-08 19:01:25 -07001185 if(target_use_signed_kernel())
1186 {
1187 read_device_info(&device);
1188
Shashank Mittal162244e2011-08-08 19:01:25 -07001189 }
1190
Greg Griscod6250552011-06-29 14:40:23 -07001191 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001192 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001193 surf_udc_device.serialno = sn_buf;
1194
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001195 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001196 if (keys_get_state(KEY_HOME) != 0)
1197 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001198 if (keys_get_state(KEY_VOLUMEUP) != 0)
1199 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001200 if(!boot_into_recovery)
1201 {
1202 if (keys_get_state(KEY_BACK) != 0)
1203 goto fastboot;
1204 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1205 goto fastboot;
1206 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001207
1208 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001209 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001210 goto fastboot;
1211 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001212
Ajay Dudani77421292010-10-27 19:34:06 -07001213 reboot_mode = check_reboot_mode();
1214 if (reboot_mode == RECOVERY_MODE) {
1215 boot_into_recovery = 1;
1216 } else if(reboot_mode == FASTBOOT_MODE) {
1217 goto fastboot;
1218 }
1219
Shashank Mittal23b8f422010-04-16 19:27:21 -07001220 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001221 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001222 if(emmc_recovery_init())
1223 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001224 if(target_use_signed_kernel())
1225 {
1226 if((device.is_unlocked) || (device.is_tampered))
1227 {
1228 #ifdef TZ_TAMPER_FUSE
1229 set_tamper_fuse_cmd();
1230 #endif
1231 }
1232 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001233 boot_linux_from_mmc();
1234 }
1235 else
1236 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001237 recovery_init();
1238 boot_linux_from_flash();
1239 }
Dima Zavinb4283602009-01-26 16:36:57 -08001240 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1241 "to fastboot mode.\n");
1242
1243fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001244
Shashank Mittal162244e2011-08-08 19:01:25 -07001245 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001246
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001247 if(!usb_init)
1248 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001249
1250 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001251
Shashank Mittal23b8f422010-04-16 19:27:21 -07001252 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001253 {
1254 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001255 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001256 }
1257 else
1258 {
1259 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001260 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001261 }
1262
1263 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001264 fastboot_register("reboot", cmd_reboot);
1265 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001266 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001267 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001268 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001269 fastboot_publish("kernel", "lk");
Kinson Chikf1a43512011-07-14 11:28:39 -07001270 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001271 sz = target_get_max_flash_size();
1272 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001273 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001274}
1275
1276APP_START(aboot)
1277 .init = aboot_init,
1278APP_END
1279