blob: f2d1169c6b7a9a707e6c38b17830580716ee9f7f [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
110
111
Dima Zavin42168f22009-01-30 11:52:22 -0800112static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
113{
114 struct atag_ptbl_entry atag_ptn;
115
Greg Griscod6250552011-06-29 14:40:23 -0700116 if (ptn->type == TYPE_MODEM_PARTITION) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800117 return;
Greg Griscod6250552011-06-29 14:40:23 -0700118 }
Dima Zavin42168f22009-01-30 11:52:22 -0800119 memcpy(atag_ptn.name, ptn->name, 16);
120 atag_ptn.name[15] = '\0';
121 atag_ptn.offset = ptn->start;
122 atag_ptn.size = ptn->length;
123 atag_ptn.flags = ptn->flags;
124 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
125 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
126}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700128void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129 const char *cmdline, unsigned machtype,
130 void *ramdisk, unsigned ramdisk_size)
131{
132 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800133 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800134 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800135 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800136 int cmdline_len = 0;
137 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700138 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800139
140 /* CORE */
141 *ptr++ = 2;
142 *ptr++ = 0x54410001;
143
144 if (ramdisk_size) {
145 *ptr++ = 4;
146 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800147 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800148 *ptr++ = ramdisk_size;
149 }
150
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800151 ptr = target_atag_mem(ptr);
152
David Ng183a7422009-12-07 14:55:21 -0800153 if (!target_is_emmc_boot()) {
154 /* Skip NAND partition ATAGS for eMMC boot */
155 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
156 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800157 for(i=0; i < ptable->count; i++) {
158 struct ptentry *ptn;
159 ptn = ptable_get(ptable, i);
160 if (ptn->type == TYPE_APPS_PARTITION)
161 pcount++;
162 }
163 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800164 sizeof(unsigned)));
165 *ptr++ = 0x4d534d70;
166 for (i = 0; i < ptable->count; ++i)
167 ptentry_to_tag(&ptr, ptable_get(ptable, i));
168 }
Dima Zavin42168f22009-01-30 11:52:22 -0800169 }
170
Brian Swetland9c4c0752009-01-25 16:23:50 -0800171 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800172 cmdline_len = strlen(cmdline);
173 have_cmdline = 1;
174 }
175 if (target_is_emmc_boot()) {
176 cmdline_len += strlen(emmc_cmdline);
177 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800178
179 cmdline_len += strlen(usb_sn_cmdline);
180 cmdline_len += strlen(sn_buf);
181
David Ngf773dde2010-07-26 19:55:08 -0700182 if (target_pause_for_battery_charge()) {
183 pause_at_bootup = 1;
184 cmdline_len += strlen(battchg_pause);
185 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800186
Shashank Mittalcd98d472011-08-02 14:29:24 -0700187 if(target_use_signed_kernel() && auth_kernel_img) {
188 cmdline_len += strlen(auth_kernel);
189 }
190
Ajay Dudanid04110c2011-01-17 23:55:07 -0800191 /* Determine correct androidboot.baseband to use */
192 switch(target_baseband())
193 {
194 case BASEBAND_APQ:
195 cmdline_len += strlen(baseband_apq);
196 break;
197
198 case BASEBAND_MSM:
199 cmdline_len += strlen(baseband_msm);
200 break;
201
202 case BASEBAND_CSFB:
203 cmdline_len += strlen(baseband_csfb);
204 break;
205
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800206 case BASEBAND_SVLTE2A:
207 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800208 break;
209 }
210
David Ng183a7422009-12-07 14:55:21 -0800211 if (cmdline_len > 0) {
212 const char *src;
213 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800214 unsigned n;
215 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800216 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800217 *ptr++ = (n / 4) + 2;
218 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800219 dst = (char *)ptr;
220 if (have_cmdline) {
221 src = cmdline;
222 while ((*dst++ = *src++));
223 }
224 if (target_is_emmc_boot()) {
225 src = emmc_cmdline;
226 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700227 have_cmdline = 1;
228 while ((*dst++ = *src++));
229 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800230
231 src = usb_sn_cmdline;
232 if (have_cmdline) --dst;
233 have_cmdline = 1;
234 while ((*dst++ = *src++));
235 src = sn_buf;
236 if (have_cmdline) --dst;
237 have_cmdline = 1;
238 while ((*dst++ = *src++));
239
David Ngf773dde2010-07-26 19:55:08 -0700240 if (pause_at_bootup) {
241 src = battchg_pause;
242 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800243 while ((*dst++ = *src++));
244 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800245
Shashank Mittalcd98d472011-08-02 14:29:24 -0700246 if(target_use_signed_kernel() && auth_kernel_img) {
247 src = auth_kernel;
248 if (have_cmdline) --dst;
249 while ((*dst++ = *src++));
250 }
251
Ajay Dudanid04110c2011-01-17 23:55:07 -0800252 switch(target_baseband())
253 {
254 case BASEBAND_APQ:
255 src = baseband_apq;
256 if (have_cmdline) --dst;
257 while ((*dst++ = *src++));
258 break;
259
260 case BASEBAND_MSM:
261 src = baseband_msm;
262 if (have_cmdline) --dst;
263 while ((*dst++ = *src++));
264 break;
265
266 case BASEBAND_CSFB:
267 src = baseband_csfb;
268 if (have_cmdline) --dst;
269 while ((*dst++ = *src++));
270 break;
271
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800272 case BASEBAND_SVLTE2A:
273 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800274 if (have_cmdline) --dst;
275 while ((*dst++ = *src++));
276 break;
277 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800278 ptr += (n / 4);
279 }
280
281 /* END */
282 *ptr++ = 0;
283 *ptr++ = 0;
284
285 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
286 kernel, ramdisk, ramdisk_size);
287 if (cmdline)
288 dprintf(INFO, "cmdline: %s\n", cmdline);
289
290 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700291 /* do any platform specific cleanup before kernel entry */
292 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800293 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800294 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800295 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800296}
297
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800298unsigned page_size = 0;
299unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800300
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800301#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800302
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800303static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800304
Shashank Mittal23b8f422010-04-16 19:27:21 -0700305int boot_linux_from_mmc(void)
306{
307 struct boot_img_hdr *hdr = (void*) buf;
308 struct boot_img_hdr *uhdr;
309 unsigned offset = 0;
310 unsigned long long ptn = 0;
311 unsigned n = 0;
312 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700313 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700314
Shashank Mittalcd98d472011-08-02 14:29:24 -0700315 unsigned char *image_addr = 0;
316 unsigned kernel_actual;
317 unsigned ramdisk_actual;
318 unsigned imagesize_actual;
319
Shashank Mittal23b8f422010-04-16 19:27:21 -0700320 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
321 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
322 dprintf(INFO, "Unified boot method!\n");
323 hdr = uhdr;
324 goto unified_boot;
325 }
Greg Griscod6250552011-06-29 14:40:23 -0700326 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700327 index = partition_get_index("boot");
328 ptn = partition_get_offset(index);
329 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700330 dprintf(CRITICAL, "ERROR: No boot partition found\n");
331 return -1;
332 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700333 }
334 else {
335 index = partition_get_index("recovery");
336 ptn = partition_get_offset(index);
337 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700338 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
339 return -1;
340 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700341 }
342
Greg Griscod6250552011-06-29 14:40:23 -0700343 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700344 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
345 return -1;
346 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700347
348 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700349 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700350 return -1;
351 }
352
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700353 if (hdr->page_size && (hdr->page_size != page_size)) {
354 page_size = hdr->page_size;
355 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700356 }
357
Shashank Mittalcd98d472011-08-02 14:29:24 -0700358 /* Authenticate Kernel */
Shashank Mittal162244e2011-08-08 19:01:25 -0700359 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_rooted))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800360 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700361 image_addr = (unsigned char *)target_get_scratch_address();
362 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
363 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
364 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
365
366 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700367
368 /* Assuming device rooted at this time */
369 device.is_rooted = 1;
370
Shashank Mittalcd98d472011-08-02 14:29:24 -0700371 /* Read image without signature */
372 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
373 {
374 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
375 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800376 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700377
378 offset = imagesize_actual;
379 /* Read signature */
380 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
381 {
382 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
383 }
384 else
385 {
386 auth_kernel_img = image_verify((unsigned char *)image_addr,
387 (unsigned char *)(image_addr + imagesize_actual),
388 imagesize_actual,
389 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700390
391 if(auth_kernel_img)
392 {
393 /* Authorized kernel */
394 device.is_rooted = 0;
395 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700396 }
397
398 /* Move kernel and ramdisk to correct address */
399 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
400 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700401
402 /* Make sure everything from scratch address is read before next step!*/
403 if(device.is_rooted)
404 {
405 write_device_info_mmc(&device);
406 #ifdef TZ_TAMPER_FUSE
407 set_tamper_fuse_cmd();
408 #endif
409 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700410 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700411 else
412 {
413 offset += page_size;
414
415 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
416 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
417 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
418 return -1;
419 }
420 offset += n;
421
422 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
423 if(n != 0)
424 {
425 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
426 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
427 return -1;
428 }
429 }
430 offset += n;
431 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700432
433unified_boot:
434 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
435 hdr->kernel_size);
436 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
437 hdr->ramdisk_size);
438
439 if(hdr->cmdline[0]) {
440 cmdline = (char*) hdr->cmdline;
441 } else {
442 cmdline = DEFAULT_CMDLINE;
443 }
444 dprintf(INFO, "cmdline = '%s'\n", cmdline);
445
446 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700447 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700448 (const char *)cmdline, board_machtype(),
449 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
450
451 return 0;
452}
453
Dima Zavin214cc642009-01-26 11:16:21 -0800454int boot_linux_from_flash(void)
455{
456 struct boot_img_hdr *hdr = (void*) buf;
457 unsigned n;
458 struct ptentry *ptn;
459 struct ptable *ptable;
460 unsigned offset = 0;
461 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800462
Shashank Mittalcd98d472011-08-02 14:29:24 -0700463 unsigned char *image_addr = 0;
464 unsigned kernel_actual;
465 unsigned ramdisk_actual;
466 unsigned imagesize_actual;
467
David Ng183a7422009-12-07 14:55:21 -0800468 if (target_is_emmc_boot()) {
469 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
470 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
471 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
472 return -1;
473 }
474 goto continue_boot;
475 }
476
Dima Zavin214cc642009-01-26 11:16:21 -0800477 ptable = flash_get_ptable();
478 if (ptable == NULL) {
479 dprintf(CRITICAL, "ERROR: Partition table not found\n");
480 return -1;
481 }
482
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800483 if(!boot_into_recovery)
484 {
485 ptn = ptable_find(ptable, "boot");
486 if (ptn == NULL) {
487 dprintf(CRITICAL, "ERROR: No boot partition found\n");
488 return -1;
489 }
490 }
491 else
492 {
493 ptn = ptable_find(ptable, "recovery");
494 if (ptn == NULL) {
495 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
496 return -1;
497 }
Dima Zavin214cc642009-01-26 11:16:21 -0800498 }
499
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800500 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800501 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
502 return -1;
503 }
Dima Zavin214cc642009-01-26 11:16:21 -0800504
505 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700506 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800507 return -1;
508 }
509
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800510 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700511 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 -0800512 return -1;
513 }
514
Shashank Mittalcd98d472011-08-02 14:29:24 -0700515 /* Authenticate Kernel */
Shashank Mittal162244e2011-08-08 19:01:25 -0700516 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_rooted))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700517 {
518 image_addr = (unsigned char *)target_get_scratch_address();
519 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
520 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
521 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800522
Shashank Mittalcd98d472011-08-02 14:29:24 -0700523 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700524
525 /* Assuming device rooted at this time */
526 device.is_rooted = 1;
527
Shashank Mittalcd98d472011-08-02 14:29:24 -0700528 /* Read image without signature */
529 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
530 {
531 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
532 return -1;
533 }
Dima Zavin214cc642009-01-26 11:16:21 -0800534
Shashank Mittalcd98d472011-08-02 14:29:24 -0700535 offset = imagesize_actual;
536 /* Read signature */
537 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
538 {
539 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
540 }
541 else
542 {
543
544 /* Verify signature */
545 auth_kernel_img = image_verify((unsigned char *)image_addr,
546 (unsigned char *)(image_addr + imagesize_actual),
547 imagesize_actual,
548 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700549
550 if(auth_kernel_img)
551 {
552 /* Authorized kernel */
553 device.is_rooted = 0;
554 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700555 }
556
557 /* Move kernel and ramdisk to correct address */
558 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
559 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700560
561 /* Make sure everything from scratch address is read before next step!*/
562 if(device.is_rooted)
563 {
564 write_device_info_flash(&device);
565 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700566 }
567 else
568 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700569 offset = page_size;
570
Shashank Mittalcd98d472011-08-02 14:29:24 -0700571 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
572 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
573 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
574 return -1;
575 }
576 offset += n;
577
578 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
579 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
580 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
581 return -1;
582 }
583 offset += n;
584 }
David Ng183a7422009-12-07 14:55:21 -0800585continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800586 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
587 hdr->kernel_size);
588 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
589 hdr->ramdisk_size);
590
591 if(hdr->cmdline[0]) {
592 cmdline = (char*) hdr->cmdline;
593 } else {
594 cmdline = DEFAULT_CMDLINE;
595 }
596 dprintf(INFO, "cmdline = '%s'\n", cmdline);
597
598 /* TODO: create/pass atags to kernel */
599
600 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700601 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800602 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800603 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
604
605 return 0;
606}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800607
Shashank Mittal162244e2011-08-08 19:01:25 -0700608unsigned char info_buf[4096];
609void write_device_info_mmc(device_info *dev)
610{
611 struct device_info *info = (void*) info_buf;
612 unsigned long long ptn = 0;
613 unsigned long long size;
614 int index = INVALID_PTN;
615
616 index = partition_get_index("aboot");
617 ptn = partition_get_offset(index);
618 if(ptn == 0)
619 {
620 return;
621 }
622
623 size = partition_get_size(index);
624
625 memcpy(info, dev, sizeof(device_info));
626
627 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
628 {
629 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
630 return;
631 }
632}
633
634void read_device_info_mmc(device_info *dev)
635{
636 struct device_info *info = (void*) info_buf;
637 unsigned long long ptn = 0;
638 unsigned long long size;
639 int index = INVALID_PTN;
640
641 index = partition_get_index("aboot");
642 ptn = partition_get_offset(index);
643 if(ptn == 0)
644 {
645 return;
646 }
647
648 size = partition_get_size(index);
649
650 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
651 {
652 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
653 return;
654 }
655
656 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
657 {
658 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
659 info->is_unlocked = 0;
660 info->is_rooted = 0;
661
662 write_device_info_mmc(info);
663 }
664 memcpy(dev, info, sizeof(device_info));
665}
666
667void write_device_info_flash(device_info *dev)
668{
669 struct device_info *info = (void *) info_buf;
670 struct ptentry *ptn;
671 struct ptable *ptable;
672
673 ptable = flash_get_ptable();
674 if (ptable == NULL)
675 {
676 dprintf(CRITICAL, "ERROR: Partition table not found\n");
677 return;
678 }
679
680 ptn = ptable_find(ptable, "devinfo");
681 if (ptn == NULL)
682 {
683 dprintf(CRITICAL, "ERROR: No boot partition found\n");
684 return;
685 }
686
687 memcpy(info, dev, sizeof(device_info));
688
689 if (flash_write(ptn, 0, (void *)info_buf, page_size))
690 {
691 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
692 return;
693 }
694}
695
696void read_device_info_flash(device_info *dev)
697{
698 struct device_info *info = (void*) info_buf;
699 struct ptentry *ptn;
700 struct ptable *ptable;
701
702 ptable = flash_get_ptable();
703 if (ptable == NULL)
704 {
705 dprintf(CRITICAL, "ERROR: Partition table not found\n");
706 return;
707 }
708
709 ptn = ptable_find(ptable, "devinfo");
710 if (ptn == NULL)
711 {
712 dprintf(CRITICAL, "ERROR: No boot partition found\n");
713 return;
714 }
715
716 if (flash_read(ptn, 0, (void *)info_buf, page_size))
717 {
718 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
719 return;
720 }
721
722 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
723 {
724 while(1);
725 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
726 info->is_unlocked = 0;
727 info->is_rooted = 0;
728 write_device_info_flash(info);
729 }
730 memcpy(dev, info, sizeof(device_info));
731}
732
733void write_device_info(device_info *dev)
734{
735 if(target_is_emmc_boot())
736 {
737 write_device_info_mmc(dev);
738 }
739 else
740 {
741 write_device_info_flash(dev);
742 }
743}
744
745void read_device_info(device_info *dev)
746{
747 if(target_is_emmc_boot())
748 {
749 read_device_info_mmc(dev);
750 }
751 else
752 {
753 read_device_info_flash(dev);
754 }
755}
756
757void reset_device_info()
758{
759 dprintf(ALWAYS, "reset_device_info called.");
760 device.is_rooted = 0;
761 write_device_info(&device);
762}
763
764void set_device_root()
765{
766 dprintf(ALWAYS, "set_device_root called.");
767 device.is_rooted = 1;
768 write_device_info(&device);
769}
770
Brian Swetland9c4c0752009-01-25 16:23:50 -0800771void cmd_boot(const char *arg, void *data, unsigned sz)
772{
773 unsigned kernel_actual;
774 unsigned ramdisk_actual;
775 static struct boot_img_hdr hdr;
776 char *ptr = ((char*) data);
Shashank Mittal162244e2011-08-08 19:01:25 -0700777 unsigned image_actual;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800778
779 if (sz < sizeof(hdr)) {
780 fastboot_fail("invalid bootimage header");
781 return;
782 }
783
784 memcpy(&hdr, data, sizeof(hdr));
785
786 /* ensure commandline is terminated */
787 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
788
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700789 if(target_is_emmc_boot() && hdr.page_size) {
790 page_size = hdr.page_size;
791 page_mask = page_size - 1;
792 }
793
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800794 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
795 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Shashank Mittal162244e2011-08-08 19:01:25 -0700796 image_actual = page_size + kernel_actual + ramdisk_actual;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800797
Shashank Mittal162244e2011-08-08 19:01:25 -0700798 if(target_use_signed_kernel())
799 image_actual += page_size;
800
801 if (image_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800802 fastboot_fail("incomplete bootimage");
803 return;
804 }
805
Ajay Dudanie28a6072011-07-01 13:59:46 -0700806 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
807 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800808
809 fastboot_okay("");
810 udc_stop();
811
Amol Jadie67872e2011-06-27 14:14:11 -0700812 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800813 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700814 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800815}
816
Dima Zavin214cc642009-01-26 11:16:21 -0800817void cmd_erase(const char *arg, void *data, unsigned sz)
818{
819 struct ptentry *ptn;
820 struct ptable *ptable;
821
822 ptable = flash_get_ptable();
823 if (ptable == NULL) {
824 fastboot_fail("partition table doesn't exist");
825 return;
826 }
827
828 ptn = ptable_find(ptable, arg);
829 if (ptn == NULL) {
830 fastboot_fail("unknown partition name");
831 return;
832 }
833
834 if (flash_erase(ptn)) {
835 fastboot_fail("failed to erase partition");
836 return;
837 }
838 fastboot_okay("");
839}
840
Bikas Gurungd48bd242010-09-04 19:54:32 -0700841
842void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
843{
844 unsigned long long ptn = 0;
845 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -0700846 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700847
Kinson Chikf1a43512011-07-14 11:28:39 -0700848 index = partition_get_index(arg);
849 ptn = partition_get_offset(index);
850 if(ptn == 0) {
Bikas Gurungd48bd242010-09-04 19:54:32 -0700851 fastboot_fail("partition table doesn't exist");
852 return;
853 }
854
Bikas Gurungd48bd242010-09-04 19:54:32 -0700855 /* Simple inefficient version of erase. Just writing
856 0 in first block */
857 if (mmc_write(ptn , 512, (unsigned int *)out)) {
858 fastboot_fail("failed to erase partition");
859 return;
860 }
861 fastboot_okay("");
862}
863
864
Ajay Dudani5c761132011-04-07 20:19:04 -0700865void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700866{
867 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700868 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700869 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700870
Greg Grisco6e754772011-06-23 12:19:39 -0700871 if (!strcmp(arg, "partition"))
872 {
873 dprintf(INFO, "Attempt to write partition image.\n");
874 if (mmc_write_partition(sz, (unsigned char *) data)) {
875 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700876 return;
877 }
878 }
Greg Grisco6e754772011-06-23 12:19:39 -0700879 else
880 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700881 index = partition_get_index(arg);
882 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700883 if(ptn == 0) {
884 fastboot_fail("partition table doesn't exist");
885 return;
886 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700887
Greg Grisco6e754772011-06-23 12:19:39 -0700888 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
889 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
890 fastboot_fail("image is not a boot image");
891 return;
892 }
893 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700894
Kinson Chikf1a43512011-07-14 11:28:39 -0700895 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700896 if (ROUND_TO_PAGE(sz,511) > size) {
897 fastboot_fail("size too large");
898 return;
899 }
900 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
901 fastboot_fail("flash write failure");
902 return;
903 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700904 }
905 fastboot_okay("");
906 return;
907}
908
Ajay Dudani5c761132011-04-07 20:19:04 -0700909void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
910{
911 unsigned int chunk;
912 unsigned int chunk_data_sz;
913 sparse_header_t *sparse_header;
914 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700915 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700916 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700917 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700918
Kinson Chikf1a43512011-07-14 11:28:39 -0700919 index = partition_get_index(arg);
920 ptn = partition_get_offset(index);
921 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700922 fastboot_fail("partition table doesn't exist");
923 return;
924 }
925
926 /* Read and skip over sparse image header */
927 sparse_header = (sparse_header_t *) data;
928 data += sparse_header->file_hdr_sz;
929 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
930 {
931 /* Skip the remaining bytes in a header that is longer than
932 * we expected.
933 */
934 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
935 }
936
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700937 dprintf (SPEW, "=== Sparse Image Header ===\n");
938 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
939 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
940 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
941 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
942 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
943 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
944 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
945 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700946
947 /* Start processing chunks */
948 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
949 {
950 /* Read and skip over chunk header */
951 chunk_header = (chunk_header_t *) data;
952 data += sizeof(chunk_header_t);
953
954 dprintf (SPEW, "=== Chunk Header ===\n");
955 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
956 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
957 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
958
959 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
960 {
961 /* Skip the remaining bytes in a header that is longer than
962 * we expected.
963 */
964 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
965 }
966
967 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
968 switch (chunk_header->chunk_type)
969 {
970 case CHUNK_TYPE_RAW:
971 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
972 chunk_data_sz))
973 {
974 fastboot_fail("Bogus chunk size for chunk type Raw");
975 return;
976 }
977
Ajay Dudaniab18f022011-05-12 14:39:22 -0700978 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
979 chunk_data_sz,
980 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700981 {
982 fastboot_fail("flash write failure");
983 return;
984 }
985 total_blocks += chunk_header->chunk_sz;
986 data += chunk_data_sz;
987 break;
988
989 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700990 total_blocks += chunk_header->chunk_sz;
991 break;
992
Ajay Dudani5c761132011-04-07 20:19:04 -0700993 case CHUNK_TYPE_CRC:
994 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
995 {
996 fastboot_fail("Bogus chunk size for chunk type Dont Care");
997 return;
998 }
999 total_blocks += chunk_header->chunk_sz;
1000 data += chunk_data_sz;
1001 break;
1002
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001003 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001004 fastboot_fail("Unknown chunk type");
1005 return;
1006 }
1007 }
1008
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001009 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1010 total_blocks, sparse_header->total_blks);
1011
1012 if(total_blocks != sparse_header->total_blks)
1013 {
1014 fastboot_fail("sparse image write failure");
1015 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001016
1017 fastboot_okay("");
1018 return;
1019}
1020
1021void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1022{
1023 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001024 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1025 unsigned int *magic_number = (unsigned int *) data;
1026 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001027
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001028 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
1029 magic_number[1] == SSD_HEADER_MAGIC_1)
1030 {
1031#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -07001032 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001033#endif
Greg Griscod6250552011-06-29 14:40:23 -07001034 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001035 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1036 return;
1037 }
1038 }
1039 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001040 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1041 cmd_flash_mmc_img(arg, data, sz);
1042 else
1043 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001044 return;
1045}
1046
Dima Zavin214cc642009-01-26 11:16:21 -08001047void cmd_flash(const char *arg, void *data, unsigned sz)
1048{
1049 struct ptentry *ptn;
1050 struct ptable *ptable;
1051 unsigned extra = 0;
1052
1053 ptable = flash_get_ptable();
1054 if (ptable == NULL) {
1055 fastboot_fail("partition table doesn't exist");
1056 return;
1057 }
1058
1059 ptn = ptable_find(ptable, arg);
1060 if (ptn == NULL) {
1061 fastboot_fail("unknown partition name");
1062 return;
1063 }
1064
1065 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1066 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1067 fastboot_fail("image is not a boot image");
1068 return;
1069 }
1070 }
1071
Chandan Uddarajud6d45042010-02-24 21:12:45 -08001072 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301073 || !strcmp(ptn->name, "persist")) {
1074 if (flash_ecc_bch_enabled())
1075 /* Spare data bytes for 8 bit ECC increased by 4 */
1076 extra = ((page_size >> 9) * 20);
1077 else
1078 extra = ((page_size >> 9) * 16);
1079 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001080 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001081
1082 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1083 if (flash_write(ptn, extra, data, sz)) {
1084 fastboot_fail("flash write failure");
1085 return;
1086 }
1087 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1088 fastboot_okay("");
1089}
1090
1091void cmd_continue(const char *arg, void *data, unsigned sz)
1092{
1093 fastboot_okay("");
1094 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001095 if (target_is_emmc_boot())
1096 {
1097 boot_linux_from_mmc();
1098 }
1099 else
1100 {
1101 boot_linux_from_flash();
1102 }
Dima Zavin214cc642009-01-26 11:16:21 -08001103}
1104
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001105void cmd_reboot(const char *arg, void *data, unsigned sz)
1106{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001107 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001108 fastboot_okay("");
1109 reboot_device(0);
1110}
1111
1112void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1113{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001114 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001115 fastboot_okay("");
1116 reboot_device(FASTBOOT_MODE);
1117}
1118
Shashank Mittal162244e2011-08-08 19:01:25 -07001119void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1120{
1121 if(!device.is_unlocked)
1122 {
1123 device.is_unlocked = 1;
1124 write_device_info(&device);
1125 }
1126 fastboot_okay("");
1127}
1128
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001129void splash_screen ()
1130{
1131 struct ptentry *ptn;
1132 struct ptable *ptable;
1133 struct fbcon_config *fb_display = NULL;
1134
1135 if (!target_is_emmc_boot())
1136 {
1137 ptable = flash_get_ptable();
1138 if (ptable == NULL) {
1139 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001140 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001141 }
1142
1143 ptn = ptable_find(ptable, "splash");
1144 if (ptn == NULL) {
1145 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1146 } else {
1147 fb_display = fbcon_display();
1148 if (fb_display) {
1149 if (flash_read(ptn, 0, fb_display->base,
1150 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1151 fbcon_clear();
1152 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1153 }
1154 }
1155 }
1156 }
1157}
1158
Brian Swetland9c4c0752009-01-25 16:23:50 -08001159void aboot_init(const struct app_descriptor *app)
1160{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001161 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001162 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001163 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001164
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001165 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001166 if (target_is_emmc_boot())
1167 {
1168 page_size = 2048;
1169 page_mask = page_size - 1;
1170 }
1171 else
1172 {
1173 page_size = flash_page_size();
1174 page_mask = page_size - 1;
1175 }
1176
Shashank Mittal162244e2011-08-08 19:01:25 -07001177 if(target_use_signed_kernel())
1178 {
1179 read_device_info(&device);
1180
1181 if((device.is_unlocked) || (device.is_rooted))
1182 {
1183 #ifdef TZ_TAMPER_FUSE
1184 set_tamper_fuse_cmd();
1185 #endif
1186 }
1187 }
1188
Greg Griscod6250552011-06-29 14:40:23 -07001189 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001190 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001191 surf_udc_device.serialno = sn_buf;
1192
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001193 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001194 if (keys_get_state(KEY_HOME) != 0)
1195 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001196 if (keys_get_state(KEY_VOLUMEUP) != 0)
1197 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001198 if(!boot_into_recovery)
1199 {
1200 if (keys_get_state(KEY_BACK) != 0)
1201 goto fastboot;
1202 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1203 goto fastboot;
1204 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001205
1206 #if NO_KEYPAD_DRIVER
1207 /* With no keypad implementation, check the status of USB connection. */
1208 /* If USB is connected then go into fastboot mode. */
1209 usb_init = 1;
1210 udc_init(&surf_udc_device);
1211 if (usb_cable_status())
1212 goto fastboot;
1213 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001214
Ajay Dudani77421292010-10-27 19:34:06 -07001215 reboot_mode = check_reboot_mode();
1216 if (reboot_mode == RECOVERY_MODE) {
1217 boot_into_recovery = 1;
1218 } else if(reboot_mode == FASTBOOT_MODE) {
1219 goto fastboot;
1220 }
1221
Shashank Mittal23b8f422010-04-16 19:27:21 -07001222 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001223 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001224 if(emmc_recovery_init())
1225 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001226 boot_linux_from_mmc();
1227 }
1228 else
1229 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001230 recovery_init();
1231 boot_linux_from_flash();
1232 }
Dima Zavinb4283602009-01-26 16:36:57 -08001233 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1234 "to fastboot mode.\n");
1235
1236fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001237
Shashank Mittal162244e2011-08-08 19:01:25 -07001238 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001239
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001240 if(!usb_init)
1241 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001242
1243 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001244
Shashank Mittal23b8f422010-04-16 19:27:21 -07001245 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001246 {
1247 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001248 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001249 }
1250 else
1251 {
1252 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001253 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001254 }
1255
1256 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001257 fastboot_register("reboot", cmd_reboot);
1258 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001259 fastboot_register("oem unlock", cmd_oem_unlock);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001260 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001261 fastboot_publish("kernel", "lk");
Shashank Mittal162244e2011-08-08 19:01:25 -07001262 fastboot_publish("root-flag", device.is_rooted);
1263 fastboot_publish("unlock-flag", device.is_unlocked);
Kinson Chikf1a43512011-07-14 11:28:39 -07001264 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001265 sz = target_get_max_flash_size();
1266 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001267 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001268}
1269
1270APP_START(aboot)
1271 .init = aboot_init,
1272APP_END
1273