| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442 |
- /*
- * Copyright (c) 2009, Google Inc.
- * All rights reserved.
- *
- * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of Code Aurora nor
- * the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written
- * permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #include <app.h>
- #include <debug.h>
- #include <arch/arm.h>
- #include <dev/udc.h>
- #include <string.h>
- #include <kernel/thread.h>
- #include <arch/ops.h>
- #include <dev/flash.h>
- #include <lib/ptable.h>
- #include <dev/keys.h>
- #include <dev/fbcon.h>
- #include <baseband.h>
- #include <target.h>
- #include <mmc.h>
- #include <partition_parser.h>
- #include <platform.h>
- #include <crypto_hash.h>
- #include "image_verify.h"
- #include "recovery.h"
- #include "bootimg.h"
- #include "fastboot.h"
- #include "sparse_format.h"
- #include "mmc.h"
- #include "devinfo.h"
- #include "scm.h"
- #define EXPAND(NAME) #NAME
- #define TARGET(NAME) EXPAND(NAME)
- #define DEFAULT_CMDLINE "mem=100M console=null";
- #ifdef MEMBASE
- #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
- #else
- #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
- #endif
- #define RECOVERY_MODE 0x77665502
- #define FASTBOOT_MODE 0x77665500
- static const char *emmc_cmdline = " androidboot.emmc=true";
- static const char *usb_sn_cmdline = " androidboot.serialno=";
- static const char *battchg_pause = " androidboot.mode=charger";
- static const char *auth_kernel = " androidboot.authorized_kernel=true";
- static const char *baseband_apq = " androidboot.baseband=apq";
- static const char *baseband_msm = " androidboot.baseband=msm";
- static const char *baseband_csfb = " androidboot.baseband=csfb";
- static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
- static const char *baseband_mdm = " androidboot.baseband=mdm";
- static const char *baseband_sglte = " androidboot.baseband=sglte";
- /* Assuming unauthorized kernel image by default */
- static int auth_kernel_img = 0;
- static device_info device = {DEVICE_MAGIC, 0, 0};
- static struct udc_device surf_udc_device = {
- .vendor_id = 0x18d1,
- .product_id = 0xD00D,
- .version_id = 0x0100,
- .manufacturer = "Google",
- .product = "Android",
- };
- struct atag_ptbl_entry
- {
- char name[16];
- unsigned offset;
- unsigned size;
- unsigned flags;
- };
- char sn_buf[13];
- extern int emmc_recovery_init(void);
- #if NO_KEYPAD_DRIVER
- extern int fastboot_trigger(void);
- #endif
- /* Koshi, 2012.06.12 { */
- static struct ptable *flash_ptable = NULL;
- struct ptable *flash_get_ptable(void)
- {
- return flash_ptable;
- }
- unsigned partition_get_index(const char *name)
- {
- return 0;
- }
- /* Get offset of the partition */
- unsigned long long partition_get_offset(int index)
- {
- return 0;
- }
- /* Get size of the partition */
- unsigned long long partition_get_size(int index)
- {
- return 0;
- }
- /*
- * MMC write function
- */
- unsigned int mmc_write(unsigned long long data_addr, unsigned int data_len, unsigned int *in)
- {
- return 0;
- }
- unsigned int write_partition(unsigned size, unsigned char *partition)
- {
- return 0;
- }
- unsigned *target_atag_mem(unsigned *ptr)
- {
- return 0;
- }
- int udc_stop(void)
- {
- return 0;
- }
- unsigned board_machtype(void)
- {
- return 0;
- }
- int udc_init(struct udc_device *dev)
- {
- return 0;
- }
- void partition_dump()
- {
- }
- int udc_start(void)
- {
- return 0;
- }
- unsigned flash_page_size(void)
- {
- return 0;
- }
- int udc_request_queue(struct udc_endpoint *ept, struct udc_request *_req)
- {
- return 0;
- }
- struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt)
- {
- return 0;
- }
- struct udc_request *udc_request_alloc(void)
- {
- return 0;
- }
- void udc_request_free(struct udc_request *req)
- {
- }
- int udc_register_gadget(struct udc_gadget *gadget)
- {
- return 0;
- }
- void udc_endpoint_free(struct udc_endpoint *ept)
- {
- /* todo */
- }
- int flash_read_ext(struct ptentry *ptn, unsigned extra_per_page, unsigned offset, void *data, unsigned bytes)
- {
- return 0;
- }
- int flash_write(struct ptentry *ptn, unsigned extra_per_page, const void *data, unsigned bytes)
- {
- return 0;
- }
- /* Koshi, 2012.06.12 } */
- static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
- {
- struct atag_ptbl_entry atag_ptn;
- memcpy(atag_ptn.name, ptn->name, 16);
- atag_ptn.name[15] = '\0';
- atag_ptn.offset = ptn->start;
- atag_ptn.size = ptn->length;
- atag_ptn.flags = ptn->flags;
- memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
- *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
- }
- void boot_linux(void *kernel, unsigned *tags,
- const char *cmdline, unsigned machtype,
- void *ramdisk, unsigned ramdisk_size)
- {
- unsigned *ptr = tags;
- unsigned pcount = 0;
- void (*entry)(unsigned,unsigned,unsigned*) = kernel;
- struct ptable *ptable;
- int cmdline_len = 0;
- int have_cmdline = 0;
- int pause_at_bootup = 0;
- unsigned char *cmdline_final = NULL;
- /* CORE */
- *ptr++ = 2;
- *ptr++ = 0x54410001;
- if (ramdisk_size) {
- *ptr++ = 4;
- *ptr++ = 0x54420005;
- *ptr++ = (unsigned)ramdisk;
- *ptr++ = ramdisk_size;
- }
- ptr = target_atag_mem(ptr);
- if (!target_is_emmc_boot()) {
- /* Skip NAND partition ATAGS for eMMC boot */
- if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
- int i;
- *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
- sizeof(unsigned)));
- *ptr++ = 0x4d534d70;
- for (i = 0; i < ptable->count; ++i)
- ptentry_to_tag(&ptr, ptable_get(ptable, i));
- }
- }
- if (cmdline && cmdline[0]) {
- cmdline_len = strlen(cmdline);
- have_cmdline = 1;
- }
- if (target_is_emmc_boot()) {
- cmdline_len += strlen(emmc_cmdline);
- }
- cmdline_len += strlen(usb_sn_cmdline);
- cmdline_len += strlen(sn_buf);
- if (target_pause_for_battery_charge()) {
- pause_at_bootup = 1;
- cmdline_len += strlen(battchg_pause);
- }
- if(target_use_signed_kernel() && auth_kernel_img) {
- cmdline_len += strlen(auth_kernel);
- }
- /* Determine correct androidboot.baseband to use */
- switch(target_baseband())
- {
- case BASEBAND_APQ:
- cmdline_len += strlen(baseband_apq);
- break;
- case BASEBAND_MSM:
- cmdline_len += strlen(baseband_msm);
- break;
- case BASEBAND_CSFB:
- cmdline_len += strlen(baseband_csfb);
- break;
- case BASEBAND_SVLTE2A:
- cmdline_len += strlen(baseband_svlte2a);
- break;
- case BASEBAND_MDM:
- cmdline_len += strlen(baseband_mdm);
- break;
- case BASEBAND_SGLTE:
- cmdline_len += strlen(baseband_sglte);
- break;
- }
- if (cmdline_len > 0) {
- const char *src;
- char *dst;
- unsigned n;
- /* include terminating 0 and round up to a word multiple */
- n = (cmdline_len + 4) & (~3);
- *ptr++ = (n / 4) + 2;
- *ptr++ = 0x54410009;
- dst = (char *)ptr;
- /* Save start ptr for debug print */
- cmdline_final = (char *)ptr;
- if (have_cmdline) {
- src = cmdline;
- while ((*dst++ = *src++));
- }
- if (target_is_emmc_boot()) {
- src = emmc_cmdline;
- if (have_cmdline) --dst;
- have_cmdline = 1;
- while ((*dst++ = *src++));
- }
- src = usb_sn_cmdline;
- if (have_cmdline) --dst;
- have_cmdline = 1;
- while ((*dst++ = *src++));
- src = sn_buf;
- if (have_cmdline) --dst;
- have_cmdline = 1;
- while ((*dst++ = *src++));
- if (pause_at_bootup) {
- src = battchg_pause;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- }
- if(target_use_signed_kernel() && auth_kernel_img) {
- src = auth_kernel;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- }
- switch(target_baseband())
- {
- case BASEBAND_APQ:
- src = baseband_apq;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- break;
- case BASEBAND_MSM:
- src = baseband_msm;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- break;
- case BASEBAND_CSFB:
- src = baseband_csfb;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- break;
- case BASEBAND_SVLTE2A:
- src = baseband_svlte2a;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- break;
- case BASEBAND_MDM:
- src = baseband_mdm;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- break;
- case BASEBAND_SGLTE:
- src = baseband_sglte;
- if (have_cmdline) --dst;
- while ((*dst++ = *src++));
- break;
- }
- ptr += (n / 4);
- }
- /* END */
- *ptr++ = 0;
- *ptr++ = 0;
- dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
- kernel, ramdisk, ramdisk_size);
- if (cmdline_final)
- dprintf(INFO, "cmdline: %s\n", cmdline_final);
- enter_critical_section();
- /* do any platform specific cleanup before kernel entry */
- platform_uninit();
- arch_disable_cache(UCACHE);
- arch_disable_mmu();
- entry(0, machtype, tags);
- }
- unsigned page_size = 0;
- unsigned page_mask = 0;
- #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
- static unsigned char buf[4096]; //Equal to max-supported pagesize
- int boot_linux_from_mmc(void)
- {
- struct boot_img_hdr *hdr = (void*) buf;
- struct boot_img_hdr *uhdr;
- unsigned offset = 0;
- unsigned long long ptn = 0;
- unsigned n = 0;
- const char *cmdline;
- int index = INVALID_PTN;
- unsigned char *image_addr = 0;
- unsigned kernel_actual;
- unsigned ramdisk_actual;
- unsigned imagesize_actual;
- uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
- if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- dprintf(INFO, "Unified boot method!\n");
- hdr = uhdr;
- goto unified_boot;
- }
- if (!boot_into_recovery) {
- index = partition_get_index("boot");
- ptn = partition_get_offset(index);
- if(ptn == 0) {
- dprintf(CRITICAL, "ERROR: No boot partition found\n");
- return -1;
- }
- }
- else {
- index = partition_get_index("recovery");
- ptn = partition_get_offset(index);
- if(ptn == 0) {
- dprintf(CRITICAL, "ERROR: No recovery partition found\n");
- return -1;
- }
- }
- if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
- dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
- return -1;
- }
- if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
- return -1;
- }
- if (hdr->page_size && (hdr->page_size != page_size)) {
- page_size = hdr->page_size;
- page_mask = page_size - 1;
- }
- /* Authenticate Kernel */
- if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
- {
- image_addr = (unsigned char *)target_get_scratch_address();
- kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
- ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
- imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
- offset = 0;
- /* Assuming device rooted at this time */
- device.is_tampered = 1;
- /* Read image without signature */
- if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
- {
- dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
- return -1;
- }
- offset = imagesize_actual;
- /* Read signature */
- if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
- {
- dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
- }
- else
- {
- auth_kernel_img = image_verify((unsigned char *)image_addr,
- (unsigned char *)(image_addr + imagesize_actual),
- imagesize_actual,
- CRYPTO_AUTH_ALG_SHA256);
- if(auth_kernel_img)
- {
- /* Authorized kernel */
- device.is_tampered = 0;
- }
- }
- /* Move kernel and ramdisk to correct address */
- memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
- memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
- /* Make sure everything from scratch address is read before next step!*/
- if(device.is_tampered)
- {
- write_device_info_mmc(&device);
- #ifdef TZ_TAMPER_FUSE
- set_tamper_fuse_cmd();
- #endif
- }
- #if USE_PCOM_SECBOOT
- set_tamper_flag(device.is_tampered);
- #endif
- }
- else
- {
- offset += page_size;
- n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
- if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
- dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
- return -1;
- }
- offset += n;
- n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
- if(n != 0)
- {
- if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
- dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
- return -1;
- }
- }
- offset += n;
- }
- unified_boot:
- dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
- hdr->kernel_size);
- dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
- hdr->ramdisk_size);
- if(hdr->cmdline[0]) {
- cmdline = (char*) hdr->cmdline;
- } else {
- cmdline = DEFAULT_CMDLINE;
- }
- dprintf(INFO, "cmdline = '%s'\n", cmdline);
- dprintf(INFO, "\nBooting Linux\n");
- boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
- (const char *)cmdline, board_machtype(),
- (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
- return 0;
- }
- int boot_linux_from_flash(void)
- {
- struct boot_img_hdr *hdr = (void*) buf;
- unsigned n;
- struct ptentry *ptn;
- struct ptable *ptable;
- unsigned offset = 0;
- const char *cmdline;
- unsigned char *image_addr = 0;
- unsigned kernel_actual;
- unsigned ramdisk_actual;
- unsigned imagesize_actual;
- if (target_is_emmc_boot()) {
- hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
- if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
- return -1;
- }
- goto continue_boot;
- }
- ptable = flash_get_ptable();
- if (ptable == NULL) {
- dprintf(CRITICAL, "ERROR: Partition table not found\n");
- return -1;
- }
- if(!boot_into_recovery)
- {
- ptn = ptable_find(ptable, "boot");
- if (ptn == NULL) {
- dprintf(CRITICAL, "ERROR: No boot partition found\n");
- return -1;
- }
- }
- else
- {
- ptn = ptable_find(ptable, "recovery");
- if (ptn == NULL) {
- dprintf(CRITICAL, "ERROR: No recovery partition found\n");
- return -1;
- }
- }
- if (flash_read(ptn, offset, buf, page_size)) {
- dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
- return -1;
- }
- if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
- return -1;
- }
- if (hdr->page_size != page_size) {
- dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
- return -1;
- }
- /* Authenticate Kernel */
- if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
- {
- image_addr = (unsigned char *)target_get_scratch_address();
- kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
- ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
- imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
- offset = 0;
- /* Assuming device rooted at this time */
- device.is_tampered = 1;
- /* Read image without signature */
- if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
- {
- dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
- return -1;
- }
- offset = imagesize_actual;
- /* Read signature */
- if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
- {
- dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
- }
- else
- {
- /* Verify signature */
- auth_kernel_img = image_verify((unsigned char *)image_addr,
- (unsigned char *)(image_addr + imagesize_actual),
- imagesize_actual,
- CRYPTO_AUTH_ALG_SHA256);
- if(auth_kernel_img)
- {
- /* Authorized kernel */
- device.is_tampered = 0;
- }
- }
- /* Move kernel and ramdisk to correct address */
- memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
- memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
- /* Make sure everything from scratch address is read before next step!*/
- if(device.is_tampered)
- {
- write_device_info_flash(&device);
- }
- #if USE_PCOM_SECBOOT
- set_tamper_flag(device.is_tampered);
- #endif
- }
- else
- {
- offset = page_size;
- n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
- if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
- dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
- return -1;
- }
- offset += n;
- n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
- if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
- dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
- return -1;
- }
- offset += n;
- }
- continue_boot:
- dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
- hdr->kernel_size);
- dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
- hdr->ramdisk_size);
- if(hdr->cmdline[0]) {
- cmdline = (char*) hdr->cmdline;
- } else {
- cmdline = DEFAULT_CMDLINE;
- }
- dprintf(INFO, "cmdline = '%s'\n", cmdline);
- /* TODO: create/pass atags to kernel */
- dprintf(INFO, "\nBooting Linux\n");
- boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
- (const char *)cmdline, board_machtype(),
- (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
- return 0;
- }
- unsigned char info_buf[4096];
- void write_device_info_mmc(device_info *dev)
- {
- struct device_info *info = (void*) info_buf;
- unsigned long long ptn = 0;
- unsigned long long size;
- int index = INVALID_PTN;
- index = partition_get_index("aboot");
- ptn = partition_get_offset(index);
- if(ptn == 0)
- {
- return;
- }
- size = partition_get_size(index);
- memcpy(info, dev, sizeof(device_info));
- if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
- {
- dprintf(CRITICAL, "ERROR: Cannot write device info\n");
- return;
- }
- }
- void read_device_info_mmc(device_info *dev)
- {
- struct device_info *info = (void*) info_buf;
- unsigned long long ptn = 0;
- unsigned long long size;
- int index = INVALID_PTN;
- index = partition_get_index("aboot");
- ptn = partition_get_offset(index);
- if(ptn == 0)
- {
- return;
- }
- size = partition_get_size(index);
- if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
- {
- dprintf(CRITICAL, "ERROR: Cannot read device info\n");
- return;
- }
- if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
- {
- memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
- info->is_unlocked = 0;
- info->is_tampered = 0;
- write_device_info_mmc(info);
- }
- memcpy(dev, info, sizeof(device_info));
- }
- void write_device_info_flash(device_info *dev)
- {
- struct device_info *info = (void *) info_buf;
- struct ptentry *ptn;
- struct ptable *ptable;
- ptable = flash_get_ptable();
- if (ptable == NULL)
- {
- dprintf(CRITICAL, "ERROR: Partition table not found\n");
- return;
- }
- ptn = ptable_find(ptable, "devinfo");
- if (ptn == NULL)
- {
- dprintf(CRITICAL, "ERROR: No boot partition found\n");
- return;
- }
- memcpy(info, dev, sizeof(device_info));
- if (flash_write(ptn, 0, (void *)info_buf, page_size))
- {
- dprintf(CRITICAL, "ERROR: Cannot write device info\n");
- return;
- }
- }
- void read_device_info_flash(device_info *dev)
- {
- struct device_info *info = (void*) info_buf;
- struct ptentry *ptn;
- struct ptable *ptable;
- ptable = flash_get_ptable();
- if (ptable == NULL)
- {
- dprintf(CRITICAL, "ERROR: Partition table not found\n");
- return;
- }
- ptn = ptable_find(ptable, "devinfo");
- if (ptn == NULL)
- {
- dprintf(CRITICAL, "ERROR: No boot partition found\n");
- return;
- }
- if (flash_read(ptn, 0, (void *)info_buf, page_size))
- {
- dprintf(CRITICAL, "ERROR: Cannot write device info\n");
- return;
- }
- if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
- {
- memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
- info->is_unlocked = 0;
- info->is_tampered = 0;
- write_device_info_flash(info);
- }
- memcpy(dev, info, sizeof(device_info));
- }
- void write_device_info(device_info *dev)
- {
- if(target_is_emmc_boot())
- {
- write_device_info_mmc(dev);
- }
- else
- {
- write_device_info_flash(dev);
- }
- }
- void read_device_info(device_info *dev)
- {
- if(target_is_emmc_boot())
- {
- read_device_info_mmc(dev);
- }
- else
- {
- read_device_info_flash(dev);
- }
- }
- void reset_device_info()
- {
- dprintf(ALWAYS, "reset_device_info called.");
- device.is_tampered = 0;
- write_device_info(&device);
- }
- void set_device_root()
- {
- dprintf(ALWAYS, "set_device_root called.");
- device.is_tampered = 1;
- write_device_info(&device);
- }
- void cmd_boot(const char *arg, void *data, unsigned sz)
- {
- unsigned kernel_actual;
- unsigned ramdisk_actual;
- static struct boot_img_hdr hdr;
- char *ptr = ((char*) data);
- if (sz < sizeof(hdr)) {
- fastboot_fail("invalid bootimage header");
- return;
- }
- memcpy(&hdr, data, sizeof(hdr));
- /* ensure commandline is terminated */
- hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
- if(target_is_emmc_boot() && hdr.page_size) {
- page_size = hdr.page_size;
- page_mask = page_size - 1;
- }
- kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
- ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
- /* sz should have atleast raw boot image */
- if (page_size + kernel_actual + ramdisk_actual > sz) {
- fastboot_fail("incomplete bootimage");
- return;
- }
- memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
- memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
- fastboot_okay("");
- udc_stop();
- boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
- (const char*) hdr.cmdline, board_machtype(),
- (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
- }
- void cmd_erase(const char *arg, void *data, unsigned sz)
- {
- struct ptentry *ptn;
- struct ptable *ptable;
- ptable = flash_get_ptable();
- if (ptable == NULL) {
- fastboot_fail("partition table doesn't exist");
- return;
- }
- ptn = ptable_find(ptable, arg);
- if (ptn == NULL) {
- fastboot_fail("unknown partition name");
- return;
- }
- if (flash_erase(ptn)) {
- fastboot_fail("failed to erase partition");
- return;
- }
- fastboot_okay("");
- }
- void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
- {
- unsigned long long ptn = 0;
- unsigned int out[512] = {0};
- int index = INVALID_PTN;
- index = partition_get_index(arg);
- ptn = partition_get_offset(index);
- if(ptn == 0) {
- fastboot_fail("Partition table doesn't exist\n");
- return;
- }
- /* Simple inefficient version of erase. Just writing
- 0 in first block */
- if (mmc_write(ptn , 512, (unsigned int *)out)) {
- fastboot_fail("failed to erase partition");
- return;
- }
- fastboot_okay("");
- }
- void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
- {
- unsigned long long ptn = 0;
- unsigned long long size = 0;
- int index = INVALID_PTN;
- if (!strcmp(arg, "partition"))
- {
- dprintf(INFO, "Attempt to write partition image.\n");
- if (write_partition(sz, (unsigned char *) data)) {
- fastboot_fail("failed to write partition");
- return;
- }
- }
- else
- {
- index = partition_get_index(arg);
- ptn = partition_get_offset(index);
- if(ptn == 0) {
- fastboot_fail("partition table doesn't exist");
- return;
- }
- if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
- if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- fastboot_fail("image is not a boot image");
- return;
- }
- }
- size = partition_get_size(index);
- if (ROUND_TO_PAGE(sz,511) > size) {
- fastboot_fail("size too large");
- return;
- }
- else if (mmc_write(ptn , sz, (unsigned int *)data)) {
- fastboot_fail("flash write failure");
- return;
- }
- }
- fastboot_okay("");
- return;
- }
- void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
- {
- unsigned int chunk;
- unsigned int chunk_data_sz;
- sparse_header_t *sparse_header;
- chunk_header_t *chunk_header;
- uint32_t total_blocks = 0;
- unsigned long long ptn = 0;
- unsigned long long size = 0;
- int index = INVALID_PTN;
- index = partition_get_index(arg);
- ptn = partition_get_offset(index);
- if(ptn == 0) {
- fastboot_fail("partition table doesn't exist");
- return;
- }
- size = partition_get_size(index);
- if (ROUND_TO_PAGE(sz,511) > size) {
- fastboot_fail("size too large");
- return;
- }
- /* Read and skip over sparse image header */
- sparse_header = (sparse_header_t *) data;
- data += sparse_header->file_hdr_sz;
- if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
- {
- /* Skip the remaining bytes in a header that is longer than
- * we expected.
- */
- data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
- }
- dprintf (SPEW, "=== Sparse Image Header ===\n");
- dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
- dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
- dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
- dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
- dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
- dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
- dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
- dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
- /* Start processing chunks */
- for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
- {
- /* Read and skip over chunk header */
- chunk_header = (chunk_header_t *) data;
- data += sizeof(chunk_header_t);
- dprintf (SPEW, "=== Chunk Header ===\n");
- dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
- dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
- dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
- if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
- {
- /* Skip the remaining bytes in a header that is longer than
- * we expected.
- */
- data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
- }
- chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
- switch (chunk_header->chunk_type)
- {
- case CHUNK_TYPE_RAW:
- if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
- chunk_data_sz))
- {
- fastboot_fail("Bogus chunk size for chunk type Raw");
- return;
- }
- if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
- chunk_data_sz,
- (unsigned int*)data))
- {
- fastboot_fail("flash write failure");
- return;
- }
- total_blocks += chunk_header->chunk_sz;
- data += chunk_data_sz;
- break;
- case CHUNK_TYPE_DONT_CARE:
- total_blocks += chunk_header->chunk_sz;
- break;
- case CHUNK_TYPE_CRC:
- if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
- {
- fastboot_fail("Bogus chunk size for chunk type Dont Care");
- return;
- }
- total_blocks += chunk_header->chunk_sz;
- data += chunk_data_sz;
- break;
- default:
- fastboot_fail("Unknown chunk type");
- return;
- }
- }
- dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
- total_blocks, sparse_header->total_blks);
- if(total_blocks != sparse_header->total_blks)
- {
- fastboot_fail("sparse image write failure");
- }
- fastboot_okay("");
- return;
- }
- void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
- {
- sparse_header_t *sparse_header;
- /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
- unsigned int *magic_number = (unsigned int *) data;
- int ret=0;
- if (magic_number[0] == DECRYPT_MAGIC_0 &&
- magic_number[1] == DECRYPT_MAGIC_1)
- {
- #ifdef SSD_ENABLE
- ret = decrypt_scm((uint32 **) &data, &sz);
- #endif
- if (ret != 0) {
- dprintf(CRITICAL, "ERROR: Invalid secure image\n");
- return;
- }
- }
- else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
- magic_number[1] == ENCRYPT_MAGIC_1)
- {
- #ifdef SSD_ENABLE
- ret = encrypt_scm((uint32 **) &data, &sz);
- #endif
- if (ret != 0) {
- dprintf(CRITICAL, "ERROR: Encryption Failure\n");
- return;
- }
- }
- sparse_header = (sparse_header_t *) data;
- if (sparse_header->magic != SPARSE_HEADER_MAGIC)
- cmd_flash_mmc_img(arg, data, sz);
- else
- cmd_flash_mmc_sparse_img(arg, data, sz);
- return;
- }
- void cmd_flash(const char *arg, void *data, unsigned sz)
- {
- struct ptentry *ptn;
- struct ptable *ptable;
- unsigned extra = 0;
- ptable = flash_get_ptable();
- if (ptable == NULL) {
- fastboot_fail("partition table doesn't exist");
- return;
- }
- ptn = ptable_find(ptable, arg);
- if (ptn == NULL) {
- fastboot_fail("unknown partition name");
- return;
- }
- if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
- if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- fastboot_fail("image is not a boot image");
- return;
- }
- }
- if (!strcmp(ptn->name, "system")
- || !strcmp(ptn->name, "userdata")
- || !strcmp(ptn->name, "persist")
- || !strcmp(ptn->name, "recoveryfs")) {
- if (flash_ecc_bch_enabled())
- /* Spare data bytes for 8 bit ECC increased by 4 */
- extra = ((page_size >> 9) * 20);
- else
- extra = ((page_size >> 9) * 16);
- } else
- sz = ROUND_TO_PAGE(sz, page_mask);
- dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
- if (flash_write(ptn, extra, data, sz)) {
- fastboot_fail("flash write failure");
- return;
- }
- dprintf(INFO, "partition '%s' updated\n", ptn->name);
- fastboot_okay("");
- }
- void cmd_continue(const char *arg, void *data, unsigned sz)
- {
- fastboot_okay("");
- udc_stop();
- if (target_is_emmc_boot())
- {
- boot_linux_from_mmc();
- }
- else
- {
- boot_linux_from_flash();
- }
- }
- void cmd_reboot(const char *arg, void *data, unsigned sz)
- {
- dprintf(INFO, "rebooting the device\n");
- fastboot_okay("");
- reboot_device(0);
- }
- void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
- {
- dprintf(INFO, "rebooting the device\n");
- fastboot_okay("");
- reboot_device(FASTBOOT_MODE);
- }
- void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
- {
- if(!device.is_unlocked)
- {
- device.is_unlocked = 1;
- write_device_info(&device);
- }
- fastboot_okay("");
- }
- void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
- {
- char response[64];
- snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
- fastboot_info(response);
- snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
- fastboot_info(response);
- fastboot_okay("");
- }
- void splash_screen ()
- {
- struct ptentry *ptn;
- struct ptable *ptable;
- struct fbcon_config *fb_display = NULL;
- if (!target_is_emmc_boot())
- {
- ptable = flash_get_ptable();
- if (ptable == NULL) {
- dprintf(CRITICAL, "ERROR: Partition table not found\n");
- return;
- }
- ptn = ptable_find(ptable, "splash");
- if (ptn == NULL) {
- dprintf(CRITICAL, "ERROR: No splash partition found\n");
- } else {
- fb_display = fbcon_display();
- if (fb_display) {
- if (flash_read(ptn, 0, fb_display->base,
- (fb_display->width * fb_display->height * fb_display->bpp/8))) {
- fbcon_clear();
- dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
- }
- }
- }
- }
- }
- void aboot_init(const struct app_descriptor *app)
- {
- unsigned reboot_mode = 0;
- unsigned usb_init = 0;
- unsigned sz = 0;
- /* Setup page size information for nand/emmc reads */
- if (target_is_emmc_boot())
- {
- page_size = 2048;
- page_mask = page_size - 1;
- }
- else
- {
- page_size = flash_page_size();
- page_mask = page_size - 1;
- }
- if(target_use_signed_kernel())
- {
- read_device_info(&device);
- }
- target_serialno((unsigned char *) sn_buf);
- dprintf(SPEW,"serial number: %s\n",sn_buf);
- surf_udc_device.serialno = sn_buf;
- /* Check if we should do something other than booting up */
- if (keys_get_state(KEY_HOME) != 0)
- boot_into_recovery = 1;
- if (keys_get_state(KEY_VOLUMEUP) != 0)
- boot_into_recovery = 1;
- if(!boot_into_recovery)
- {
- if (keys_get_state(KEY_BACK) != 0)
- goto fastboot;
- if (keys_get_state(KEY_VOLUMEDOWN) != 0)
- goto fastboot;
- }
- #if NO_KEYPAD_DRIVER
- if (fastboot_trigger())
- goto fastboot;
- #endif
- reboot_mode = check_reboot_mode();
- if (reboot_mode == RECOVERY_MODE) {
- boot_into_recovery = 1;
- } else if(reboot_mode == FASTBOOT_MODE) {
- goto fastboot;
- }
- if (target_is_emmc_boot())
- {
- if(emmc_recovery_init())
- dprintf(ALWAYS,"error in emmc_recovery_init\n");
- if(target_use_signed_kernel())
- {
- if((device.is_unlocked) || (device.is_tampered))
- {
- #ifdef TZ_TAMPER_FUSE
- set_tamper_fuse_cmd();
- #endif
- #if USE_PCOM_SECBOOT
- set_tamper_flag(device.is_tampered);
- #endif
- }
- }
- boot_linux_from_mmc();
- }
- else
- {
- recovery_init();
- #if USE_PCOM_SECBOOT
- if((device.is_unlocked) || (device.is_tampered))
- set_tamper_flag(device.is_tampered);
- #endif
- boot_linux_from_flash();
- }
- dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
- "to fastboot mode.\n");
- fastboot:
- target_fastboot_init();
- if(!usb_init)
- udc_init(&surf_udc_device);
- fastboot_register("boot", cmd_boot);
- if (target_is_emmc_boot())
- {
- fastboot_register("flash:", cmd_flash_mmc);
- fastboot_register("erase:", cmd_erase_mmc);
- }
- else
- {
- fastboot_register("flash:", cmd_flash);
- fastboot_register("erase:", cmd_erase);
- }
- fastboot_register("continue", cmd_continue);
- fastboot_register("reboot", cmd_reboot);
- fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
- fastboot_register("oem unlock", cmd_oem_unlock);
- fastboot_register("oem device-info", cmd_oem_devinfo);
- fastboot_publish("product", TARGET(BOARD));
- fastboot_publish("kernel", "lk");
- fastboot_publish("serialno", sn_buf);
- partition_dump();
- sz = target_get_max_flash_size();
- fastboot_init(target_get_scratch_address(), sz);
- udc_start();
- }
- APP_START(aboot)
- .init = aboot_init,
- APP_END
|