aboot.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /*
  2. * Copyright (c) 2009, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
  6. *
  7. * 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. *
  31. */
  32. #include <app.h>
  33. #include <debug.h>
  34. #include <arch/arm.h>
  35. #include <dev/udc.h>
  36. #include <string.h>
  37. #include <kernel/thread.h>
  38. #include <arch/ops.h>
  39. #include <dev/flash.h>
  40. #include <lib/ptable.h>
  41. #include <dev/keys.h>
  42. #include <dev/fbcon.h>
  43. #include <baseband.h>
  44. #include <target.h>
  45. #include <mmc.h>
  46. #include <partition_parser.h>
  47. #include <platform.h>
  48. #include <crypto_hash.h>
  49. #include "image_verify.h"
  50. #include "recovery.h"
  51. #include "bootimg.h"
  52. #include "fastboot.h"
  53. #include "sparse_format.h"
  54. #include "mmc.h"
  55. #include "devinfo.h"
  56. #include "scm.h"
  57. #define EXPAND(NAME) #NAME
  58. #define TARGET(NAME) EXPAND(NAME)
  59. #define DEFAULT_CMDLINE "mem=100M console=null";
  60. #ifdef MEMBASE
  61. #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
  62. #else
  63. #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
  64. #endif
  65. #define RECOVERY_MODE 0x77665502
  66. #define FASTBOOT_MODE 0x77665500
  67. static const char *emmc_cmdline = " androidboot.emmc=true";
  68. static const char *usb_sn_cmdline = " androidboot.serialno=";
  69. static const char *battchg_pause = " androidboot.mode=charger";
  70. static const char *auth_kernel = " androidboot.authorized_kernel=true";
  71. static const char *baseband_apq = " androidboot.baseband=apq";
  72. static const char *baseband_msm = " androidboot.baseband=msm";
  73. static const char *baseband_csfb = " androidboot.baseband=csfb";
  74. static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
  75. static const char *baseband_mdm = " androidboot.baseband=mdm";
  76. static const char *baseband_sglte = " androidboot.baseband=sglte";
  77. /* Assuming unauthorized kernel image by default */
  78. static int auth_kernel_img = 0;
  79. static device_info device = {DEVICE_MAGIC, 0, 0};
  80. static struct udc_device surf_udc_device = {
  81. .vendor_id = 0x18d1,
  82. .product_id = 0xD00D,
  83. .version_id = 0x0100,
  84. .manufacturer = "Google",
  85. .product = "Android",
  86. };
  87. struct atag_ptbl_entry
  88. {
  89. char name[16];
  90. unsigned offset;
  91. unsigned size;
  92. unsigned flags;
  93. };
  94. char sn_buf[13];
  95. extern int emmc_recovery_init(void);
  96. #if NO_KEYPAD_DRIVER
  97. extern int fastboot_trigger(void);
  98. #endif
  99. /* Koshi, 2012.06.12 { */
  100. static struct ptable *flash_ptable = NULL;
  101. struct ptable *flash_get_ptable(void)
  102. {
  103. return flash_ptable;
  104. }
  105. unsigned partition_get_index(const char *name)
  106. {
  107. return 0;
  108. }
  109. /* Get offset of the partition */
  110. unsigned long long partition_get_offset(int index)
  111. {
  112. return 0;
  113. }
  114. /* Get size of the partition */
  115. unsigned long long partition_get_size(int index)
  116. {
  117. return 0;
  118. }
  119. /*
  120. * MMC write function
  121. */
  122. unsigned int mmc_write(unsigned long long data_addr, unsigned int data_len, unsigned int *in)
  123. {
  124. return 0;
  125. }
  126. unsigned int write_partition(unsigned size, unsigned char *partition)
  127. {
  128. return 0;
  129. }
  130. unsigned *target_atag_mem(unsigned *ptr)
  131. {
  132. return 0;
  133. }
  134. int udc_stop(void)
  135. {
  136. return 0;
  137. }
  138. unsigned board_machtype(void)
  139. {
  140. return 0;
  141. }
  142. int udc_init(struct udc_device *dev)
  143. {
  144. return 0;
  145. }
  146. void partition_dump()
  147. {
  148. }
  149. int udc_start(void)
  150. {
  151. return 0;
  152. }
  153. unsigned flash_page_size(void)
  154. {
  155. return 0;
  156. }
  157. int udc_request_queue(struct udc_endpoint *ept, struct udc_request *_req)
  158. {
  159. return 0;
  160. }
  161. struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt)
  162. {
  163. return 0;
  164. }
  165. struct udc_request *udc_request_alloc(void)
  166. {
  167. return 0;
  168. }
  169. void udc_request_free(struct udc_request *req)
  170. {
  171. }
  172. int udc_register_gadget(struct udc_gadget *gadget)
  173. {
  174. return 0;
  175. }
  176. void udc_endpoint_free(struct udc_endpoint *ept)
  177. {
  178. /* todo */
  179. }
  180. int flash_read_ext(struct ptentry *ptn, unsigned extra_per_page, unsigned offset, void *data, unsigned bytes)
  181. {
  182. return 0;
  183. }
  184. int flash_write(struct ptentry *ptn, unsigned extra_per_page, const void *data, unsigned bytes)
  185. {
  186. return 0;
  187. }
  188. /* Koshi, 2012.06.12 } */
  189. static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
  190. {
  191. struct atag_ptbl_entry atag_ptn;
  192. memcpy(atag_ptn.name, ptn->name, 16);
  193. atag_ptn.name[15] = '\0';
  194. atag_ptn.offset = ptn->start;
  195. atag_ptn.size = ptn->length;
  196. atag_ptn.flags = ptn->flags;
  197. memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
  198. *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
  199. }
  200. void boot_linux(void *kernel, unsigned *tags,
  201. const char *cmdline, unsigned machtype,
  202. void *ramdisk, unsigned ramdisk_size)
  203. {
  204. unsigned *ptr = tags;
  205. unsigned pcount = 0;
  206. void (*entry)(unsigned,unsigned,unsigned*) = kernel;
  207. struct ptable *ptable;
  208. int cmdline_len = 0;
  209. int have_cmdline = 0;
  210. int pause_at_bootup = 0;
  211. unsigned char *cmdline_final = NULL;
  212. /* CORE */
  213. *ptr++ = 2;
  214. *ptr++ = 0x54410001;
  215. if (ramdisk_size) {
  216. *ptr++ = 4;
  217. *ptr++ = 0x54420005;
  218. *ptr++ = (unsigned)ramdisk;
  219. *ptr++ = ramdisk_size;
  220. }
  221. ptr = target_atag_mem(ptr);
  222. if (!target_is_emmc_boot()) {
  223. /* Skip NAND partition ATAGS for eMMC boot */
  224. if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
  225. int i;
  226. *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
  227. sizeof(unsigned)));
  228. *ptr++ = 0x4d534d70;
  229. for (i = 0; i < ptable->count; ++i)
  230. ptentry_to_tag(&ptr, ptable_get(ptable, i));
  231. }
  232. }
  233. if (cmdline && cmdline[0]) {
  234. cmdline_len = strlen(cmdline);
  235. have_cmdline = 1;
  236. }
  237. if (target_is_emmc_boot()) {
  238. cmdline_len += strlen(emmc_cmdline);
  239. }
  240. cmdline_len += strlen(usb_sn_cmdline);
  241. cmdline_len += strlen(sn_buf);
  242. if (target_pause_for_battery_charge()) {
  243. pause_at_bootup = 1;
  244. cmdline_len += strlen(battchg_pause);
  245. }
  246. if(target_use_signed_kernel() && auth_kernel_img) {
  247. cmdline_len += strlen(auth_kernel);
  248. }
  249. /* Determine correct androidboot.baseband to use */
  250. switch(target_baseband())
  251. {
  252. case BASEBAND_APQ:
  253. cmdline_len += strlen(baseband_apq);
  254. break;
  255. case BASEBAND_MSM:
  256. cmdline_len += strlen(baseband_msm);
  257. break;
  258. case BASEBAND_CSFB:
  259. cmdline_len += strlen(baseband_csfb);
  260. break;
  261. case BASEBAND_SVLTE2A:
  262. cmdline_len += strlen(baseband_svlte2a);
  263. break;
  264. case BASEBAND_MDM:
  265. cmdline_len += strlen(baseband_mdm);
  266. break;
  267. case BASEBAND_SGLTE:
  268. cmdline_len += strlen(baseband_sglte);
  269. break;
  270. }
  271. if (cmdline_len > 0) {
  272. const char *src;
  273. char *dst;
  274. unsigned n;
  275. /* include terminating 0 and round up to a word multiple */
  276. n = (cmdline_len + 4) & (~3);
  277. *ptr++ = (n / 4) + 2;
  278. *ptr++ = 0x54410009;
  279. dst = (char *)ptr;
  280. /* Save start ptr for debug print */
  281. cmdline_final = (char *)ptr;
  282. if (have_cmdline) {
  283. src = cmdline;
  284. while ((*dst++ = *src++));
  285. }
  286. if (target_is_emmc_boot()) {
  287. src = emmc_cmdline;
  288. if (have_cmdline) --dst;
  289. have_cmdline = 1;
  290. while ((*dst++ = *src++));
  291. }
  292. src = usb_sn_cmdline;
  293. if (have_cmdline) --dst;
  294. have_cmdline = 1;
  295. while ((*dst++ = *src++));
  296. src = sn_buf;
  297. if (have_cmdline) --dst;
  298. have_cmdline = 1;
  299. while ((*dst++ = *src++));
  300. if (pause_at_bootup) {
  301. src = battchg_pause;
  302. if (have_cmdline) --dst;
  303. while ((*dst++ = *src++));
  304. }
  305. if(target_use_signed_kernel() && auth_kernel_img) {
  306. src = auth_kernel;
  307. if (have_cmdline) --dst;
  308. while ((*dst++ = *src++));
  309. }
  310. switch(target_baseband())
  311. {
  312. case BASEBAND_APQ:
  313. src = baseband_apq;
  314. if (have_cmdline) --dst;
  315. while ((*dst++ = *src++));
  316. break;
  317. case BASEBAND_MSM:
  318. src = baseband_msm;
  319. if (have_cmdline) --dst;
  320. while ((*dst++ = *src++));
  321. break;
  322. case BASEBAND_CSFB:
  323. src = baseband_csfb;
  324. if (have_cmdline) --dst;
  325. while ((*dst++ = *src++));
  326. break;
  327. case BASEBAND_SVLTE2A:
  328. src = baseband_svlte2a;
  329. if (have_cmdline) --dst;
  330. while ((*dst++ = *src++));
  331. break;
  332. case BASEBAND_MDM:
  333. src = baseband_mdm;
  334. if (have_cmdline) --dst;
  335. while ((*dst++ = *src++));
  336. break;
  337. case BASEBAND_SGLTE:
  338. src = baseband_sglte;
  339. if (have_cmdline) --dst;
  340. while ((*dst++ = *src++));
  341. break;
  342. }
  343. ptr += (n / 4);
  344. }
  345. /* END */
  346. *ptr++ = 0;
  347. *ptr++ = 0;
  348. dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
  349. kernel, ramdisk, ramdisk_size);
  350. if (cmdline_final)
  351. dprintf(INFO, "cmdline: %s\n", cmdline_final);
  352. enter_critical_section();
  353. /* do any platform specific cleanup before kernel entry */
  354. platform_uninit();
  355. arch_disable_cache(UCACHE);
  356. arch_disable_mmu();
  357. entry(0, machtype, tags);
  358. }
  359. unsigned page_size = 0;
  360. unsigned page_mask = 0;
  361. #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
  362. static unsigned char buf[4096]; //Equal to max-supported pagesize
  363. int boot_linux_from_mmc(void)
  364. {
  365. struct boot_img_hdr *hdr = (void*) buf;
  366. struct boot_img_hdr *uhdr;
  367. unsigned offset = 0;
  368. unsigned long long ptn = 0;
  369. unsigned n = 0;
  370. const char *cmdline;
  371. int index = INVALID_PTN;
  372. unsigned char *image_addr = 0;
  373. unsigned kernel_actual;
  374. unsigned ramdisk_actual;
  375. unsigned imagesize_actual;
  376. uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
  377. if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  378. dprintf(INFO, "Unified boot method!\n");
  379. hdr = uhdr;
  380. goto unified_boot;
  381. }
  382. if (!boot_into_recovery) {
  383. index = partition_get_index("boot");
  384. ptn = partition_get_offset(index);
  385. if(ptn == 0) {
  386. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  387. return -1;
  388. }
  389. }
  390. else {
  391. index = partition_get_index("recovery");
  392. ptn = partition_get_offset(index);
  393. if(ptn == 0) {
  394. dprintf(CRITICAL, "ERROR: No recovery partition found\n");
  395. return -1;
  396. }
  397. }
  398. if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
  399. dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
  400. return -1;
  401. }
  402. if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  403. dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  404. return -1;
  405. }
  406. if (hdr->page_size && (hdr->page_size != page_size)) {
  407. page_size = hdr->page_size;
  408. page_mask = page_size - 1;
  409. }
  410. /* Authenticate Kernel */
  411. if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
  412. {
  413. image_addr = (unsigned char *)target_get_scratch_address();
  414. kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  415. ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  416. imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
  417. offset = 0;
  418. /* Assuming device rooted at this time */
  419. device.is_tampered = 1;
  420. /* Read image without signature */
  421. if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
  422. {
  423. dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
  424. return -1;
  425. }
  426. offset = imagesize_actual;
  427. /* Read signature */
  428. if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
  429. {
  430. dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
  431. }
  432. else
  433. {
  434. auth_kernel_img = image_verify((unsigned char *)image_addr,
  435. (unsigned char *)(image_addr + imagesize_actual),
  436. imagesize_actual,
  437. CRYPTO_AUTH_ALG_SHA256);
  438. if(auth_kernel_img)
  439. {
  440. /* Authorized kernel */
  441. device.is_tampered = 0;
  442. }
  443. }
  444. /* Move kernel and ramdisk to correct address */
  445. memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
  446. memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
  447. /* Make sure everything from scratch address is read before next step!*/
  448. if(device.is_tampered)
  449. {
  450. write_device_info_mmc(&device);
  451. #ifdef TZ_TAMPER_FUSE
  452. set_tamper_fuse_cmd();
  453. #endif
  454. }
  455. #if USE_PCOM_SECBOOT
  456. set_tamper_flag(device.is_tampered);
  457. #endif
  458. }
  459. else
  460. {
  461. offset += page_size;
  462. n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  463. if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
  464. dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
  465. return -1;
  466. }
  467. offset += n;
  468. n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  469. if(n != 0)
  470. {
  471. if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
  472. dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
  473. return -1;
  474. }
  475. }
  476. offset += n;
  477. }
  478. unified_boot:
  479. dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
  480. hdr->kernel_size);
  481. dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
  482. hdr->ramdisk_size);
  483. if(hdr->cmdline[0]) {
  484. cmdline = (char*) hdr->cmdline;
  485. } else {
  486. cmdline = DEFAULT_CMDLINE;
  487. }
  488. dprintf(INFO, "cmdline = '%s'\n", cmdline);
  489. dprintf(INFO, "\nBooting Linux\n");
  490. boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
  491. (const char *)cmdline, board_machtype(),
  492. (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
  493. return 0;
  494. }
  495. int boot_linux_from_flash(void)
  496. {
  497. struct boot_img_hdr *hdr = (void*) buf;
  498. unsigned n;
  499. struct ptentry *ptn;
  500. struct ptable *ptable;
  501. unsigned offset = 0;
  502. const char *cmdline;
  503. unsigned char *image_addr = 0;
  504. unsigned kernel_actual;
  505. unsigned ramdisk_actual;
  506. unsigned imagesize_actual;
  507. if (target_is_emmc_boot()) {
  508. hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
  509. if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  510. dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  511. return -1;
  512. }
  513. goto continue_boot;
  514. }
  515. ptable = flash_get_ptable();
  516. if (ptable == NULL) {
  517. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  518. return -1;
  519. }
  520. if(!boot_into_recovery)
  521. {
  522. ptn = ptable_find(ptable, "boot");
  523. if (ptn == NULL) {
  524. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  525. return -1;
  526. }
  527. }
  528. else
  529. {
  530. ptn = ptable_find(ptable, "recovery");
  531. if (ptn == NULL) {
  532. dprintf(CRITICAL, "ERROR: No recovery partition found\n");
  533. return -1;
  534. }
  535. }
  536. if (flash_read(ptn, offset, buf, page_size)) {
  537. dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
  538. return -1;
  539. }
  540. if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  541. dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  542. return -1;
  543. }
  544. if (hdr->page_size != page_size) {
  545. dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
  546. return -1;
  547. }
  548. /* Authenticate Kernel */
  549. if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
  550. {
  551. image_addr = (unsigned char *)target_get_scratch_address();
  552. kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  553. ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  554. imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
  555. offset = 0;
  556. /* Assuming device rooted at this time */
  557. device.is_tampered = 1;
  558. /* Read image without signature */
  559. if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
  560. {
  561. dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
  562. return -1;
  563. }
  564. offset = imagesize_actual;
  565. /* Read signature */
  566. if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
  567. {
  568. dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
  569. }
  570. else
  571. {
  572. /* Verify signature */
  573. auth_kernel_img = image_verify((unsigned char *)image_addr,
  574. (unsigned char *)(image_addr + imagesize_actual),
  575. imagesize_actual,
  576. CRYPTO_AUTH_ALG_SHA256);
  577. if(auth_kernel_img)
  578. {
  579. /* Authorized kernel */
  580. device.is_tampered = 0;
  581. }
  582. }
  583. /* Move kernel and ramdisk to correct address */
  584. memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
  585. memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
  586. /* Make sure everything from scratch address is read before next step!*/
  587. if(device.is_tampered)
  588. {
  589. write_device_info_flash(&device);
  590. }
  591. #if USE_PCOM_SECBOOT
  592. set_tamper_flag(device.is_tampered);
  593. #endif
  594. }
  595. else
  596. {
  597. offset = page_size;
  598. n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  599. if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
  600. dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
  601. return -1;
  602. }
  603. offset += n;
  604. n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  605. if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
  606. dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
  607. return -1;
  608. }
  609. offset += n;
  610. }
  611. continue_boot:
  612. dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
  613. hdr->kernel_size);
  614. dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
  615. hdr->ramdisk_size);
  616. if(hdr->cmdline[0]) {
  617. cmdline = (char*) hdr->cmdline;
  618. } else {
  619. cmdline = DEFAULT_CMDLINE;
  620. }
  621. dprintf(INFO, "cmdline = '%s'\n", cmdline);
  622. /* TODO: create/pass atags to kernel */
  623. dprintf(INFO, "\nBooting Linux\n");
  624. boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
  625. (const char *)cmdline, board_machtype(),
  626. (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
  627. return 0;
  628. }
  629. unsigned char info_buf[4096];
  630. void write_device_info_mmc(device_info *dev)
  631. {
  632. struct device_info *info = (void*) info_buf;
  633. unsigned long long ptn = 0;
  634. unsigned long long size;
  635. int index = INVALID_PTN;
  636. index = partition_get_index("aboot");
  637. ptn = partition_get_offset(index);
  638. if(ptn == 0)
  639. {
  640. return;
  641. }
  642. size = partition_get_size(index);
  643. memcpy(info, dev, sizeof(device_info));
  644. if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
  645. {
  646. dprintf(CRITICAL, "ERROR: Cannot write device info\n");
  647. return;
  648. }
  649. }
  650. void read_device_info_mmc(device_info *dev)
  651. {
  652. struct device_info *info = (void*) info_buf;
  653. unsigned long long ptn = 0;
  654. unsigned long long size;
  655. int index = INVALID_PTN;
  656. index = partition_get_index("aboot");
  657. ptn = partition_get_offset(index);
  658. if(ptn == 0)
  659. {
  660. return;
  661. }
  662. size = partition_get_size(index);
  663. if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
  664. {
  665. dprintf(CRITICAL, "ERROR: Cannot read device info\n");
  666. return;
  667. }
  668. if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
  669. {
  670. memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
  671. info->is_unlocked = 0;
  672. info->is_tampered = 0;
  673. write_device_info_mmc(info);
  674. }
  675. memcpy(dev, info, sizeof(device_info));
  676. }
  677. void write_device_info_flash(device_info *dev)
  678. {
  679. struct device_info *info = (void *) info_buf;
  680. struct ptentry *ptn;
  681. struct ptable *ptable;
  682. ptable = flash_get_ptable();
  683. if (ptable == NULL)
  684. {
  685. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  686. return;
  687. }
  688. ptn = ptable_find(ptable, "devinfo");
  689. if (ptn == NULL)
  690. {
  691. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  692. return;
  693. }
  694. memcpy(info, dev, sizeof(device_info));
  695. if (flash_write(ptn, 0, (void *)info_buf, page_size))
  696. {
  697. dprintf(CRITICAL, "ERROR: Cannot write device info\n");
  698. return;
  699. }
  700. }
  701. void read_device_info_flash(device_info *dev)
  702. {
  703. struct device_info *info = (void*) info_buf;
  704. struct ptentry *ptn;
  705. struct ptable *ptable;
  706. ptable = flash_get_ptable();
  707. if (ptable == NULL)
  708. {
  709. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  710. return;
  711. }
  712. ptn = ptable_find(ptable, "devinfo");
  713. if (ptn == NULL)
  714. {
  715. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  716. return;
  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. if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
  724. {
  725. memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
  726. info->is_unlocked = 0;
  727. info->is_tampered = 0;
  728. write_device_info_flash(info);
  729. }
  730. memcpy(dev, info, sizeof(device_info));
  731. }
  732. void write_device_info(device_info *dev)
  733. {
  734. if(target_is_emmc_boot())
  735. {
  736. write_device_info_mmc(dev);
  737. }
  738. else
  739. {
  740. write_device_info_flash(dev);
  741. }
  742. }
  743. void read_device_info(device_info *dev)
  744. {
  745. if(target_is_emmc_boot())
  746. {
  747. read_device_info_mmc(dev);
  748. }
  749. else
  750. {
  751. read_device_info_flash(dev);
  752. }
  753. }
  754. void reset_device_info()
  755. {
  756. dprintf(ALWAYS, "reset_device_info called.");
  757. device.is_tampered = 0;
  758. write_device_info(&device);
  759. }
  760. void set_device_root()
  761. {
  762. dprintf(ALWAYS, "set_device_root called.");
  763. device.is_tampered = 1;
  764. write_device_info(&device);
  765. }
  766. void cmd_boot(const char *arg, void *data, unsigned sz)
  767. {
  768. unsigned kernel_actual;
  769. unsigned ramdisk_actual;
  770. static struct boot_img_hdr hdr;
  771. char *ptr = ((char*) data);
  772. if (sz < sizeof(hdr)) {
  773. fastboot_fail("invalid bootimage header");
  774. return;
  775. }
  776. memcpy(&hdr, data, sizeof(hdr));
  777. /* ensure commandline is terminated */
  778. hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
  779. if(target_is_emmc_boot() && hdr.page_size) {
  780. page_size = hdr.page_size;
  781. page_mask = page_size - 1;
  782. }
  783. kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
  784. ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
  785. /* sz should have atleast raw boot image */
  786. if (page_size + kernel_actual + ramdisk_actual > sz) {
  787. fastboot_fail("incomplete bootimage");
  788. return;
  789. }
  790. memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
  791. memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
  792. fastboot_okay("");
  793. udc_stop();
  794. boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
  795. (const char*) hdr.cmdline, board_machtype(),
  796. (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
  797. }
  798. void cmd_erase(const char *arg, void *data, unsigned sz)
  799. {
  800. struct ptentry *ptn;
  801. struct ptable *ptable;
  802. ptable = flash_get_ptable();
  803. if (ptable == NULL) {
  804. fastboot_fail("partition table doesn't exist");
  805. return;
  806. }
  807. ptn = ptable_find(ptable, arg);
  808. if (ptn == NULL) {
  809. fastboot_fail("unknown partition name");
  810. return;
  811. }
  812. if (flash_erase(ptn)) {
  813. fastboot_fail("failed to erase partition");
  814. return;
  815. }
  816. fastboot_okay("");
  817. }
  818. void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
  819. {
  820. unsigned long long ptn = 0;
  821. unsigned int out[512] = {0};
  822. int index = INVALID_PTN;
  823. index = partition_get_index(arg);
  824. ptn = partition_get_offset(index);
  825. if(ptn == 0) {
  826. fastboot_fail("Partition table doesn't exist\n");
  827. return;
  828. }
  829. /* Simple inefficient version of erase. Just writing
  830. 0 in first block */
  831. if (mmc_write(ptn , 512, (unsigned int *)out)) {
  832. fastboot_fail("failed to erase partition");
  833. return;
  834. }
  835. fastboot_okay("");
  836. }
  837. void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
  838. {
  839. unsigned long long ptn = 0;
  840. unsigned long long size = 0;
  841. int index = INVALID_PTN;
  842. if (!strcmp(arg, "partition"))
  843. {
  844. dprintf(INFO, "Attempt to write partition image.\n");
  845. if (write_partition(sz, (unsigned char *) data)) {
  846. fastboot_fail("failed to write partition");
  847. return;
  848. }
  849. }
  850. else
  851. {
  852. index = partition_get_index(arg);
  853. ptn = partition_get_offset(index);
  854. if(ptn == 0) {
  855. fastboot_fail("partition table doesn't exist");
  856. return;
  857. }
  858. if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
  859. if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  860. fastboot_fail("image is not a boot image");
  861. return;
  862. }
  863. }
  864. size = partition_get_size(index);
  865. if (ROUND_TO_PAGE(sz,511) > size) {
  866. fastboot_fail("size too large");
  867. return;
  868. }
  869. else if (mmc_write(ptn , sz, (unsigned int *)data)) {
  870. fastboot_fail("flash write failure");
  871. return;
  872. }
  873. }
  874. fastboot_okay("");
  875. return;
  876. }
  877. void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
  878. {
  879. unsigned int chunk;
  880. unsigned int chunk_data_sz;
  881. sparse_header_t *sparse_header;
  882. chunk_header_t *chunk_header;
  883. uint32_t total_blocks = 0;
  884. unsigned long long ptn = 0;
  885. unsigned long long size = 0;
  886. int index = INVALID_PTN;
  887. index = partition_get_index(arg);
  888. ptn = partition_get_offset(index);
  889. if(ptn == 0) {
  890. fastboot_fail("partition table doesn't exist");
  891. return;
  892. }
  893. size = partition_get_size(index);
  894. if (ROUND_TO_PAGE(sz,511) > size) {
  895. fastboot_fail("size too large");
  896. return;
  897. }
  898. /* Read and skip over sparse image header */
  899. sparse_header = (sparse_header_t *) data;
  900. data += sparse_header->file_hdr_sz;
  901. if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
  902. {
  903. /* Skip the remaining bytes in a header that is longer than
  904. * we expected.
  905. */
  906. data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
  907. }
  908. dprintf (SPEW, "=== Sparse Image Header ===\n");
  909. dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
  910. dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
  911. dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
  912. dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  913. dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  914. dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
  915. dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
  916. dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
  917. /* Start processing chunks */
  918. for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
  919. {
  920. /* Read and skip over chunk header */
  921. chunk_header = (chunk_header_t *) data;
  922. data += sizeof(chunk_header_t);
  923. dprintf (SPEW, "=== Chunk Header ===\n");
  924. dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
  925. dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
  926. dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
  927. if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
  928. {
  929. /* Skip the remaining bytes in a header that is longer than
  930. * we expected.
  931. */
  932. data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
  933. }
  934. chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
  935. switch (chunk_header->chunk_type)
  936. {
  937. case CHUNK_TYPE_RAW:
  938. if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
  939. chunk_data_sz))
  940. {
  941. fastboot_fail("Bogus chunk size for chunk type Raw");
  942. return;
  943. }
  944. if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
  945. chunk_data_sz,
  946. (unsigned int*)data))
  947. {
  948. fastboot_fail("flash write failure");
  949. return;
  950. }
  951. total_blocks += chunk_header->chunk_sz;
  952. data += chunk_data_sz;
  953. break;
  954. case CHUNK_TYPE_DONT_CARE:
  955. total_blocks += chunk_header->chunk_sz;
  956. break;
  957. case CHUNK_TYPE_CRC:
  958. if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
  959. {
  960. fastboot_fail("Bogus chunk size for chunk type Dont Care");
  961. return;
  962. }
  963. total_blocks += chunk_header->chunk_sz;
  964. data += chunk_data_sz;
  965. break;
  966. default:
  967. fastboot_fail("Unknown chunk type");
  968. return;
  969. }
  970. }
  971. dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
  972. total_blocks, sparse_header->total_blks);
  973. if(total_blocks != sparse_header->total_blks)
  974. {
  975. fastboot_fail("sparse image write failure");
  976. }
  977. fastboot_okay("");
  978. return;
  979. }
  980. void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
  981. {
  982. sparse_header_t *sparse_header;
  983. /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
  984. unsigned int *magic_number = (unsigned int *) data;
  985. int ret=0;
  986. if (magic_number[0] == DECRYPT_MAGIC_0 &&
  987. magic_number[1] == DECRYPT_MAGIC_1)
  988. {
  989. #ifdef SSD_ENABLE
  990. ret = decrypt_scm((uint32 **) &data, &sz);
  991. #endif
  992. if (ret != 0) {
  993. dprintf(CRITICAL, "ERROR: Invalid secure image\n");
  994. return;
  995. }
  996. }
  997. else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
  998. magic_number[1] == ENCRYPT_MAGIC_1)
  999. {
  1000. #ifdef SSD_ENABLE
  1001. ret = encrypt_scm((uint32 **) &data, &sz);
  1002. #endif
  1003. if (ret != 0) {
  1004. dprintf(CRITICAL, "ERROR: Encryption Failure\n");
  1005. return;
  1006. }
  1007. }
  1008. sparse_header = (sparse_header_t *) data;
  1009. if (sparse_header->magic != SPARSE_HEADER_MAGIC)
  1010. cmd_flash_mmc_img(arg, data, sz);
  1011. else
  1012. cmd_flash_mmc_sparse_img(arg, data, sz);
  1013. return;
  1014. }
  1015. void cmd_flash(const char *arg, void *data, unsigned sz)
  1016. {
  1017. struct ptentry *ptn;
  1018. struct ptable *ptable;
  1019. unsigned extra = 0;
  1020. ptable = flash_get_ptable();
  1021. if (ptable == NULL) {
  1022. fastboot_fail("partition table doesn't exist");
  1023. return;
  1024. }
  1025. ptn = ptable_find(ptable, arg);
  1026. if (ptn == NULL) {
  1027. fastboot_fail("unknown partition name");
  1028. return;
  1029. }
  1030. if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
  1031. if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  1032. fastboot_fail("image is not a boot image");
  1033. return;
  1034. }
  1035. }
  1036. if (!strcmp(ptn->name, "system")
  1037. || !strcmp(ptn->name, "userdata")
  1038. || !strcmp(ptn->name, "persist")
  1039. || !strcmp(ptn->name, "recoveryfs")) {
  1040. if (flash_ecc_bch_enabled())
  1041. /* Spare data bytes for 8 bit ECC increased by 4 */
  1042. extra = ((page_size >> 9) * 20);
  1043. else
  1044. extra = ((page_size >> 9) * 16);
  1045. } else
  1046. sz = ROUND_TO_PAGE(sz, page_mask);
  1047. dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
  1048. if (flash_write(ptn, extra, data, sz)) {
  1049. fastboot_fail("flash write failure");
  1050. return;
  1051. }
  1052. dprintf(INFO, "partition '%s' updated\n", ptn->name);
  1053. fastboot_okay("");
  1054. }
  1055. void cmd_continue(const char *arg, void *data, unsigned sz)
  1056. {
  1057. fastboot_okay("");
  1058. udc_stop();
  1059. if (target_is_emmc_boot())
  1060. {
  1061. boot_linux_from_mmc();
  1062. }
  1063. else
  1064. {
  1065. boot_linux_from_flash();
  1066. }
  1067. }
  1068. void cmd_reboot(const char *arg, void *data, unsigned sz)
  1069. {
  1070. dprintf(INFO, "rebooting the device\n");
  1071. fastboot_okay("");
  1072. reboot_device(0);
  1073. }
  1074. void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
  1075. {
  1076. dprintf(INFO, "rebooting the device\n");
  1077. fastboot_okay("");
  1078. reboot_device(FASTBOOT_MODE);
  1079. }
  1080. void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
  1081. {
  1082. if(!device.is_unlocked)
  1083. {
  1084. device.is_unlocked = 1;
  1085. write_device_info(&device);
  1086. }
  1087. fastboot_okay("");
  1088. }
  1089. void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
  1090. {
  1091. char response[64];
  1092. snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
  1093. fastboot_info(response);
  1094. snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
  1095. fastboot_info(response);
  1096. fastboot_okay("");
  1097. }
  1098. void splash_screen ()
  1099. {
  1100. struct ptentry *ptn;
  1101. struct ptable *ptable;
  1102. struct fbcon_config *fb_display = NULL;
  1103. if (!target_is_emmc_boot())
  1104. {
  1105. ptable = flash_get_ptable();
  1106. if (ptable == NULL) {
  1107. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  1108. return;
  1109. }
  1110. ptn = ptable_find(ptable, "splash");
  1111. if (ptn == NULL) {
  1112. dprintf(CRITICAL, "ERROR: No splash partition found\n");
  1113. } else {
  1114. fb_display = fbcon_display();
  1115. if (fb_display) {
  1116. if (flash_read(ptn, 0, fb_display->base,
  1117. (fb_display->width * fb_display->height * fb_display->bpp/8))) {
  1118. fbcon_clear();
  1119. dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
  1120. }
  1121. }
  1122. }
  1123. }
  1124. }
  1125. void aboot_init(const struct app_descriptor *app)
  1126. {
  1127. unsigned reboot_mode = 0;
  1128. unsigned usb_init = 0;
  1129. unsigned sz = 0;
  1130. /* Setup page size information for nand/emmc reads */
  1131. if (target_is_emmc_boot())
  1132. {
  1133. page_size = 2048;
  1134. page_mask = page_size - 1;
  1135. }
  1136. else
  1137. {
  1138. page_size = flash_page_size();
  1139. page_mask = page_size - 1;
  1140. }
  1141. if(target_use_signed_kernel())
  1142. {
  1143. read_device_info(&device);
  1144. }
  1145. target_serialno((unsigned char *) sn_buf);
  1146. dprintf(SPEW,"serial number: %s\n",sn_buf);
  1147. surf_udc_device.serialno = sn_buf;
  1148. /* Check if we should do something other than booting up */
  1149. if (keys_get_state(KEY_HOME) != 0)
  1150. boot_into_recovery = 1;
  1151. if (keys_get_state(KEY_VOLUMEUP) != 0)
  1152. boot_into_recovery = 1;
  1153. if(!boot_into_recovery)
  1154. {
  1155. if (keys_get_state(KEY_BACK) != 0)
  1156. goto fastboot;
  1157. if (keys_get_state(KEY_VOLUMEDOWN) != 0)
  1158. goto fastboot;
  1159. }
  1160. #if NO_KEYPAD_DRIVER
  1161. if (fastboot_trigger())
  1162. goto fastboot;
  1163. #endif
  1164. reboot_mode = check_reboot_mode();
  1165. if (reboot_mode == RECOVERY_MODE) {
  1166. boot_into_recovery = 1;
  1167. } else if(reboot_mode == FASTBOOT_MODE) {
  1168. goto fastboot;
  1169. }
  1170. if (target_is_emmc_boot())
  1171. {
  1172. if(emmc_recovery_init())
  1173. dprintf(ALWAYS,"error in emmc_recovery_init\n");
  1174. if(target_use_signed_kernel())
  1175. {
  1176. if((device.is_unlocked) || (device.is_tampered))
  1177. {
  1178. #ifdef TZ_TAMPER_FUSE
  1179. set_tamper_fuse_cmd();
  1180. #endif
  1181. #if USE_PCOM_SECBOOT
  1182. set_tamper_flag(device.is_tampered);
  1183. #endif
  1184. }
  1185. }
  1186. boot_linux_from_mmc();
  1187. }
  1188. else
  1189. {
  1190. recovery_init();
  1191. #if USE_PCOM_SECBOOT
  1192. if((device.is_unlocked) || (device.is_tampered))
  1193. set_tamper_flag(device.is_tampered);
  1194. #endif
  1195. boot_linux_from_flash();
  1196. }
  1197. dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
  1198. "to fastboot mode.\n");
  1199. fastboot:
  1200. target_fastboot_init();
  1201. if(!usb_init)
  1202. udc_init(&surf_udc_device);
  1203. fastboot_register("boot", cmd_boot);
  1204. if (target_is_emmc_boot())
  1205. {
  1206. fastboot_register("flash:", cmd_flash_mmc);
  1207. fastboot_register("erase:", cmd_erase_mmc);
  1208. }
  1209. else
  1210. {
  1211. fastboot_register("flash:", cmd_flash);
  1212. fastboot_register("erase:", cmd_erase);
  1213. }
  1214. fastboot_register("continue", cmd_continue);
  1215. fastboot_register("reboot", cmd_reboot);
  1216. fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
  1217. fastboot_register("oem unlock", cmd_oem_unlock);
  1218. fastboot_register("oem device-info", cmd_oem_devinfo);
  1219. fastboot_publish("product", TARGET(BOARD));
  1220. fastboot_publish("kernel", "lk");
  1221. fastboot_publish("serialno", sn_buf);
  1222. partition_dump();
  1223. sz = target_get_max_flash_size();
  1224. fastboot_init(target_get_scratch_address(), sz);
  1225. udc_start();
  1226. }
  1227. APP_START(aboot)
  1228. .init = aboot_init,
  1229. APP_END