blob: e2a6174cc7e55b6df031c8080678222f3d103c93 [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;
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530915 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700916 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700917
Kinson Chikf1a43512011-07-14 11:28:39 -0700918 index = partition_get_index(arg);
919 ptn = partition_get_offset(index);
920 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700921 fastboot_fail("partition table doesn't exist");
922 return;
923 }
924
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530925 size = partition_get_size(index);
926 if (ROUND_TO_PAGE(sz,511) > size) {
927 fastboot_fail("size too large");
928 return;
929 }
930
Ajay Dudani5c761132011-04-07 20:19:04 -0700931 /* Read and skip over sparse image header */
932 sparse_header = (sparse_header_t *) data;
933 data += sparse_header->file_hdr_sz;
934 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
935 {
936 /* Skip the remaining bytes in a header that is longer than
937 * we expected.
938 */
939 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
940 }
941
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700942 dprintf (SPEW, "=== Sparse Image Header ===\n");
943 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
944 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
945 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
946 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
947 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
948 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
949 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
950 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700951
952 /* Start processing chunks */
953 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
954 {
955 /* Read and skip over chunk header */
956 chunk_header = (chunk_header_t *) data;
957 data += sizeof(chunk_header_t);
958
959 dprintf (SPEW, "=== Chunk Header ===\n");
960 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
961 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
962 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
963
964 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
965 {
966 /* Skip the remaining bytes in a header that is longer than
967 * we expected.
968 */
969 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
970 }
971
972 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
973 switch (chunk_header->chunk_type)
974 {
975 case CHUNK_TYPE_RAW:
976 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
977 chunk_data_sz))
978 {
979 fastboot_fail("Bogus chunk size for chunk type Raw");
980 return;
981 }
982
Ajay Dudaniab18f022011-05-12 14:39:22 -0700983 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
984 chunk_data_sz,
985 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700986 {
987 fastboot_fail("flash write failure");
988 return;
989 }
990 total_blocks += chunk_header->chunk_sz;
991 data += chunk_data_sz;
992 break;
993
994 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700995 total_blocks += chunk_header->chunk_sz;
996 break;
997
Ajay Dudani5c761132011-04-07 20:19:04 -0700998 case CHUNK_TYPE_CRC:
999 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1000 {
1001 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1002 return;
1003 }
1004 total_blocks += chunk_header->chunk_sz;
1005 data += chunk_data_sz;
1006 break;
1007
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001008 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001009 fastboot_fail("Unknown chunk type");
1010 return;
1011 }
1012 }
1013
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001014 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1015 total_blocks, sparse_header->total_blks);
1016
1017 if(total_blocks != sparse_header->total_blks)
1018 {
1019 fastboot_fail("sparse image write failure");
1020 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001021
1022 fastboot_okay("");
1023 return;
1024}
1025
1026void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1027{
1028 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001029 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1030 unsigned int *magic_number = (unsigned int *) data;
1031 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001032
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001033 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
1034 magic_number[1] == SSD_HEADER_MAGIC_1)
1035 {
1036#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -07001037 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001038#endif
Greg Griscod6250552011-06-29 14:40:23 -07001039 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001040 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1041 return;
1042 }
1043 }
1044 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001045 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1046 cmd_flash_mmc_img(arg, data, sz);
1047 else
1048 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001049 return;
1050}
1051
Dima Zavin214cc642009-01-26 11:16:21 -08001052void cmd_flash(const char *arg, void *data, unsigned sz)
1053{
1054 struct ptentry *ptn;
1055 struct ptable *ptable;
1056 unsigned extra = 0;
1057
1058 ptable = flash_get_ptable();
1059 if (ptable == NULL) {
1060 fastboot_fail("partition table doesn't exist");
1061 return;
1062 }
1063
1064 ptn = ptable_find(ptable, arg);
1065 if (ptn == NULL) {
1066 fastboot_fail("unknown partition name");
1067 return;
1068 }
1069
1070 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1071 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1072 fastboot_fail("image is not a boot image");
1073 return;
1074 }
1075 }
1076
Chandan Uddarajud6d45042010-02-24 21:12:45 -08001077 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301078 || !strcmp(ptn->name, "persist")) {
1079 if (flash_ecc_bch_enabled())
1080 /* Spare data bytes for 8 bit ECC increased by 4 */
1081 extra = ((page_size >> 9) * 20);
1082 else
1083 extra = ((page_size >> 9) * 16);
1084 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001085 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001086
1087 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1088 if (flash_write(ptn, extra, data, sz)) {
1089 fastboot_fail("flash write failure");
1090 return;
1091 }
1092 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1093 fastboot_okay("");
1094}
1095
1096void cmd_continue(const char *arg, void *data, unsigned sz)
1097{
1098 fastboot_okay("");
1099 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001100 if (target_is_emmc_boot())
1101 {
1102 boot_linux_from_mmc();
1103 }
1104 else
1105 {
1106 boot_linux_from_flash();
1107 }
Dima Zavin214cc642009-01-26 11:16:21 -08001108}
1109
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001110void cmd_reboot(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(0);
1115}
1116
1117void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1118{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001119 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001120 fastboot_okay("");
1121 reboot_device(FASTBOOT_MODE);
1122}
1123
Shashank Mittal162244e2011-08-08 19:01:25 -07001124void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1125{
1126 if(!device.is_unlocked)
1127 {
1128 device.is_unlocked = 1;
1129 write_device_info(&device);
1130 }
1131 fastboot_okay("");
1132}
1133
Shashank Mittala0032282011-08-26 14:50:11 -07001134void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1135{
1136 char response[64];
1137 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1138 fastboot_info(response);
1139 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1140 fastboot_info(response);
1141 fastboot_okay("");
1142}
1143
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001144void splash_screen ()
1145{
1146 struct ptentry *ptn;
1147 struct ptable *ptable;
1148 struct fbcon_config *fb_display = NULL;
1149
1150 if (!target_is_emmc_boot())
1151 {
1152 ptable = flash_get_ptable();
1153 if (ptable == NULL) {
1154 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001155 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001156 }
1157
1158 ptn = ptable_find(ptable, "splash");
1159 if (ptn == NULL) {
1160 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1161 } else {
1162 fb_display = fbcon_display();
1163 if (fb_display) {
1164 if (flash_read(ptn, 0, fb_display->base,
1165 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1166 fbcon_clear();
1167 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1168 }
1169 }
1170 }
1171 }
1172}
1173
Brian Swetland9c4c0752009-01-25 16:23:50 -08001174void aboot_init(const struct app_descriptor *app)
1175{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001176 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001177 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001178 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001179
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001180 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001181 if (target_is_emmc_boot())
1182 {
1183 page_size = 2048;
1184 page_mask = page_size - 1;
1185 }
1186 else
1187 {
1188 page_size = flash_page_size();
1189 page_mask = page_size - 1;
1190 }
1191
Shashank Mittal162244e2011-08-08 19:01:25 -07001192 if(target_use_signed_kernel())
1193 {
1194 read_device_info(&device);
1195
Shashank Mittal162244e2011-08-08 19:01:25 -07001196 }
1197
Greg Griscod6250552011-06-29 14:40:23 -07001198 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001199 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001200 surf_udc_device.serialno = sn_buf;
1201
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001202 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001203 if (keys_get_state(KEY_HOME) != 0)
1204 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001205 if (keys_get_state(KEY_VOLUMEUP) != 0)
1206 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001207 if(!boot_into_recovery)
1208 {
1209 if (keys_get_state(KEY_BACK) != 0)
1210 goto fastboot;
1211 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1212 goto fastboot;
1213 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001214
1215 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001216 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001217 goto fastboot;
1218 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001219
Ajay Dudani77421292010-10-27 19:34:06 -07001220 reboot_mode = check_reboot_mode();
1221 if (reboot_mode == RECOVERY_MODE) {
1222 boot_into_recovery = 1;
1223 } else if(reboot_mode == FASTBOOT_MODE) {
1224 goto fastboot;
1225 }
1226
Shashank Mittal23b8f422010-04-16 19:27:21 -07001227 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001228 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001229 if(emmc_recovery_init())
1230 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001231 if(target_use_signed_kernel())
1232 {
1233 if((device.is_unlocked) || (device.is_tampered))
1234 {
1235 #ifdef TZ_TAMPER_FUSE
1236 set_tamper_fuse_cmd();
1237 #endif
1238 }
1239 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001240 boot_linux_from_mmc();
1241 }
1242 else
1243 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001244 recovery_init();
1245 boot_linux_from_flash();
1246 }
Dima Zavinb4283602009-01-26 16:36:57 -08001247 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1248 "to fastboot mode.\n");
1249
1250fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001251
Shashank Mittal162244e2011-08-08 19:01:25 -07001252 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001253
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001254 if(!usb_init)
1255 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001256
1257 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001258
Shashank Mittal23b8f422010-04-16 19:27:21 -07001259 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001260 {
1261 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001262 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001263 }
1264 else
1265 {
1266 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001267 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001268 }
1269
1270 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001271 fastboot_register("reboot", cmd_reboot);
1272 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001273 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001274 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001275 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001276 fastboot_publish("kernel", "lk");
Kinson Chikf1a43512011-07-14 11:28:39 -07001277 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001278 sz = target_get_max_flash_size();
1279 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001280 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001281}
1282
1283APP_START(aboot)
1284 .init = aboot_init,
1285APP_END
1286