aboot.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  1. /*
  2. * Copyright (c) 2009, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Copyright (c) 2009-2014, The Linux Foundation. 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 The Linux Foundation 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 <string.h>
  36. #include <stdlib.h>
  37. #include <limits.h>
  38. #include <kernel/thread.h>
  39. #include <arch/ops.h>
  40. #include <dev/flash.h>
  41. #include <lib/ptable.h>
  42. #include <dev/keys.h>
  43. #include <dev/fbcon.h>
  44. #include <baseband.h>
  45. #include <target.h>
  46. #include <mmc.h>
  47. #include <partition_parser.h>
  48. #include <platform.h>
  49. #include <crypto_hash.h>
  50. #include <malloc.h>
  51. #include <boot_stats.h>
  52. #include <sha.h>
  53. #include <platform/iomap.h>
  54. #include <boot_device.h>
  55. #if DEVICE_TREE
  56. #include <libfdt.h>
  57. #include <dev_tree.h>
  58. #endif
  59. #include "image_verify.h"
  60. #include "recovery.h"
  61. #include "bootimg.h"
  62. #include "fastboot.h"
  63. #include "sparse_format.h"
  64. #include "mmc.h"
  65. #include "devinfo.h"
  66. #include "board.h"
  67. #include "scm.h"
  68. extern bool target_use_signed_kernel(void);
  69. extern void platform_uninit(void);
  70. extern void target_uninit(void);
  71. extern int get_target_boot_params(const char *cmdline, const char *part,
  72. char *buf, int buflen);
  73. void write_device_info_mmc(device_info *dev);
  74. void write_device_info_flash(device_info *dev);
  75. #define EXPAND(NAME) #NAME
  76. #define TARGET(NAME) EXPAND(NAME)
  77. #ifdef MEMBASE
  78. #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
  79. #else
  80. #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
  81. #endif
  82. #ifndef MEMSIZE
  83. #define MEMSIZE 1024*1024
  84. #endif
  85. #define MAX_TAGS_SIZE 1024
  86. #define RECOVERY_MODE 0x77665502
  87. #define FASTBOOT_MODE 0x77665500
  88. /* make 4096 as default size to ensure EFS,EXT4's erasing */
  89. #define DEFAULT_ERASE_SIZE 4096
  90. #define MAX_PANEL_BUF_SIZE 128
  91. #define UBI_MAGIC "UBI#"
  92. #define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
  93. #define UBI_MAGIC_SIZE 0x04
  94. #define BOOT_DEV_MAX_LEN 64
  95. #define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
  96. #define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
  97. #if UFS_SUPPORT
  98. static const char *emmc_cmdline = " androidboot.bootdevice=";
  99. #else
  100. static const char *emmc_cmdline = " androidboot.emmc=true";
  101. #endif
  102. static const char *usb_sn_cmdline = " androidboot.serialno=";
  103. static const char *androidboot_mode = " androidboot.mode=";
  104. static const char *loglevel = " quiet";
  105. static const char *battchg_pause = " androidboot.mode=charger";
  106. static const char *auth_kernel = " androidboot.authorized_kernel=true";
  107. static const char *secondary_gpt_enable = " gpt";
  108. static const char *baseband_apq = " androidboot.baseband=apq";
  109. static const char *baseband_msm = " androidboot.baseband=msm";
  110. static const char *baseband_csfb = " androidboot.baseband=csfb";
  111. static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
  112. static const char *baseband_mdm = " androidboot.baseband=mdm";
  113. static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
  114. static const char *baseband_sglte = " androidboot.baseband=sglte";
  115. static const char *baseband_dsda = " androidboot.baseband=dsda";
  116. static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
  117. static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
  118. static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
  119. static unsigned page_size = 0;
  120. static unsigned page_mask = 0;
  121. static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
  122. static bool boot_into_ffbm;
  123. static char target_boot_params[64];
  124. /* Assuming unauthorized kernel image by default */
  125. static int auth_kernel_img = 0;
  126. static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
  127. struct atag_ptbl_entry
  128. {
  129. char name[16];
  130. unsigned offset;
  131. unsigned size;
  132. unsigned flags;
  133. };
  134. /*
  135. * Partition info, required to be published
  136. * for fastboot
  137. */
  138. struct getvar_partition_info {
  139. const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
  140. char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
  141. char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
  142. char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
  143. char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
  144. };
  145. /*
  146. * Right now, we are publishing the info for only
  147. * three partitions
  148. */
  149. struct getvar_partition_info part_info[] =
  150. {
  151. { "system" , "partition-size:", "partition-type:", "", "ext4" },
  152. { "userdata", "partition-size:", "partition-type:", "", "ext4" },
  153. { "cache" , "partition-size:", "partition-type:", "", "ext4" },
  154. };
  155. char max_download_size[MAX_RSP_SIZE];
  156. char charger_screen_enabled[MAX_RSP_SIZE];
  157. char sn_buf[13];
  158. char display_panel_buf[MAX_PANEL_BUF_SIZE];
  159. char panel_display_mode[MAX_RSP_SIZE];
  160. extern int emmc_recovery_init(void);
  161. #if NO_KEYPAD_DRIVER
  162. extern int fastboot_trigger(void);
  163. #endif
  164. static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
  165. {
  166. /* overwrite the destination of specified for the project */
  167. #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
  168. if (is_arm64)
  169. hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
  170. else
  171. hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
  172. hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
  173. hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
  174. #endif
  175. }
  176. static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
  177. {
  178. struct atag_ptbl_entry atag_ptn;
  179. memcpy(atag_ptn.name, ptn->name, 16);
  180. atag_ptn.name[15] = '\0';
  181. atag_ptn.offset = ptn->start;
  182. atag_ptn.size = ptn->length;
  183. atag_ptn.flags = ptn->flags;
  184. memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
  185. *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
  186. }
  187. unsigned char *update_cmdline(const char * cmdline)
  188. {
  189. int cmdline_len = 0;
  190. int have_cmdline = 0;
  191. unsigned char *cmdline_final = NULL;
  192. int pause_at_bootup = 0;
  193. bool warm_boot = false;
  194. bool gpt_exists = partition_gpt_exists();
  195. int have_target_boot_params = 0;
  196. char *boot_dev_buf = NULL;
  197. if (cmdline && cmdline[0]) {
  198. cmdline_len = strlen(cmdline);
  199. have_cmdline = 1;
  200. }
  201. if (target_is_emmc_boot()) {
  202. cmdline_len += strlen(emmc_cmdline);
  203. #if UFS_SUPPORT
  204. boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
  205. ASSERT(boot_dev_buf);
  206. platform_boot_dev_cmdline(boot_dev_buf);
  207. cmdline_len += strlen(boot_dev_buf);
  208. #endif
  209. }
  210. cmdline_len += strlen(usb_sn_cmdline);
  211. cmdline_len += strlen(sn_buf);
  212. if (boot_into_recovery && gpt_exists)
  213. cmdline_len += strlen(secondary_gpt_enable);
  214. if (boot_into_ffbm) {
  215. cmdline_len += strlen(androidboot_mode);
  216. cmdline_len += strlen(ffbm_mode_string);
  217. /* reduce kernel console messages to speed-up boot */
  218. cmdline_len += strlen(loglevel);
  219. } else if (device.charger_screen_enabled &&
  220. target_pause_for_battery_charge()) {
  221. pause_at_bootup = 1;
  222. cmdline_len += strlen(battchg_pause);
  223. }
  224. if(target_use_signed_kernel() && auth_kernel_img) {
  225. cmdline_len += strlen(auth_kernel);
  226. }
  227. if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
  228. "system",
  229. target_boot_params,
  230. sizeof(target_boot_params)) == 0) {
  231. have_target_boot_params = 1;
  232. cmdline_len += strlen(target_boot_params);
  233. }
  234. /* Determine correct androidboot.baseband to use */
  235. switch(target_baseband())
  236. {
  237. case BASEBAND_APQ:
  238. cmdline_len += strlen(baseband_apq);
  239. break;
  240. case BASEBAND_MSM:
  241. cmdline_len += strlen(baseband_msm);
  242. break;
  243. case BASEBAND_CSFB:
  244. cmdline_len += strlen(baseband_csfb);
  245. break;
  246. case BASEBAND_SVLTE2A:
  247. cmdline_len += strlen(baseband_svlte2a);
  248. break;
  249. case BASEBAND_MDM:
  250. cmdline_len += strlen(baseband_mdm);
  251. break;
  252. case BASEBAND_MDM2:
  253. cmdline_len += strlen(baseband_mdm2);
  254. break;
  255. case BASEBAND_SGLTE:
  256. cmdline_len += strlen(baseband_sglte);
  257. break;
  258. case BASEBAND_SGLTE2:
  259. cmdline_len += strlen(baseband_sglte2);
  260. break;
  261. case BASEBAND_DSDA:
  262. cmdline_len += strlen(baseband_dsda);
  263. break;
  264. case BASEBAND_DSDA2:
  265. cmdline_len += strlen(baseband_dsda2);
  266. break;
  267. }
  268. if (cmdline) {
  269. if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
  270. target_display_panel_node(device.display_panel,
  271. display_panel_buf, MAX_PANEL_BUF_SIZE) &&
  272. strlen(display_panel_buf)) {
  273. cmdline_len += strlen(display_panel_buf);
  274. }
  275. }
  276. if (target_warm_boot()) {
  277. warm_boot = true;
  278. cmdline_len += strlen(warmboot_cmdline);
  279. }
  280. if (cmdline_len > 0) {
  281. const char *src;
  282. unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
  283. ASSERT(dst != NULL);
  284. /* Save start ptr for debug print */
  285. cmdline_final = dst;
  286. if (have_cmdline) {
  287. src = cmdline;
  288. while ((*dst++ = *src++));
  289. }
  290. if (target_is_emmc_boot()) {
  291. src = emmc_cmdline;
  292. if (have_cmdline) --dst;
  293. have_cmdline = 1;
  294. while ((*dst++ = *src++));
  295. #if UFS_SUPPORT
  296. src = boot_dev_buf;
  297. if (have_cmdline) --dst;
  298. while ((*dst++ = *src++));
  299. #endif
  300. }
  301. src = usb_sn_cmdline;
  302. if (have_cmdline) --dst;
  303. have_cmdline = 1;
  304. while ((*dst++ = *src++));
  305. src = sn_buf;
  306. if (have_cmdline) --dst;
  307. have_cmdline = 1;
  308. while ((*dst++ = *src++));
  309. if (warm_boot) {
  310. if (have_cmdline) --dst;
  311. src = warmboot_cmdline;
  312. while ((*dst++ = *src++));
  313. }
  314. if (boot_into_recovery && gpt_exists) {
  315. src = secondary_gpt_enable;
  316. if (have_cmdline) --dst;
  317. while ((*dst++ = *src++));
  318. }
  319. if (boot_into_ffbm) {
  320. src = androidboot_mode;
  321. if (have_cmdline) --dst;
  322. while ((*dst++ = *src++));
  323. src = ffbm_mode_string;
  324. if (have_cmdline) --dst;
  325. while ((*dst++ = *src++));
  326. src = loglevel;
  327. if (have_cmdline) --dst;
  328. while ((*dst++ = *src++));
  329. } else if (pause_at_bootup) {
  330. src = battchg_pause;
  331. if (have_cmdline) --dst;
  332. while ((*dst++ = *src++));
  333. }
  334. if(target_use_signed_kernel() && auth_kernel_img) {
  335. src = auth_kernel;
  336. if (have_cmdline) --dst;
  337. while ((*dst++ = *src++));
  338. }
  339. switch(target_baseband())
  340. {
  341. case BASEBAND_APQ:
  342. src = baseband_apq;
  343. if (have_cmdline) --dst;
  344. while ((*dst++ = *src++));
  345. break;
  346. case BASEBAND_MSM:
  347. src = baseband_msm;
  348. if (have_cmdline) --dst;
  349. while ((*dst++ = *src++));
  350. break;
  351. case BASEBAND_CSFB:
  352. src = baseband_csfb;
  353. if (have_cmdline) --dst;
  354. while ((*dst++ = *src++));
  355. break;
  356. case BASEBAND_SVLTE2A:
  357. src = baseband_svlte2a;
  358. if (have_cmdline) --dst;
  359. while ((*dst++ = *src++));
  360. break;
  361. case BASEBAND_MDM:
  362. src = baseband_mdm;
  363. if (have_cmdline) --dst;
  364. while ((*dst++ = *src++));
  365. break;
  366. case BASEBAND_MDM2:
  367. src = baseband_mdm2;
  368. if (have_cmdline) --dst;
  369. while ((*dst++ = *src++));
  370. break;
  371. case BASEBAND_SGLTE:
  372. src = baseband_sglte;
  373. if (have_cmdline) --dst;
  374. while ((*dst++ = *src++));
  375. break;
  376. case BASEBAND_SGLTE2:
  377. src = baseband_sglte2;
  378. if (have_cmdline) --dst;
  379. while ((*dst++ = *src++));
  380. break;
  381. case BASEBAND_DSDA:
  382. src = baseband_dsda;
  383. if (have_cmdline) --dst;
  384. while ((*dst++ = *src++));
  385. break;
  386. case BASEBAND_DSDA2:
  387. src = baseband_dsda2;
  388. if (have_cmdline) --dst;
  389. while ((*dst++ = *src++));
  390. break;
  391. }
  392. if (strlen(display_panel_buf)) {
  393. src = display_panel_buf;
  394. if (have_cmdline) --dst;
  395. while ((*dst++ = *src++));
  396. }
  397. if (have_target_boot_params) {
  398. if (have_cmdline) --dst;
  399. src = target_boot_params;
  400. while ((*dst++ = *src++));
  401. }
  402. }
  403. if (boot_dev_buf)
  404. free(boot_dev_buf);
  405. dprintf(INFO, "cmdline: %s\n", cmdline_final);
  406. return cmdline_final;
  407. }
  408. unsigned *atag_core(unsigned *ptr)
  409. {
  410. /* CORE */
  411. *ptr++ = 2;
  412. *ptr++ = 0x54410001;
  413. return ptr;
  414. }
  415. unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
  416. unsigned ramdisk_size)
  417. {
  418. if (ramdisk_size) {
  419. *ptr++ = 4;
  420. *ptr++ = 0x54420005;
  421. *ptr++ = (unsigned)ramdisk;
  422. *ptr++ = ramdisk_size;
  423. }
  424. return ptr;
  425. }
  426. unsigned *atag_ptable(unsigned **ptr_addr)
  427. {
  428. int i;
  429. struct ptable *ptable;
  430. if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
  431. *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
  432. sizeof(unsigned)));
  433. *(*ptr_addr)++ = 0x4d534d70;
  434. for (i = 0; i < ptable->count; ++i)
  435. ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
  436. }
  437. return (*ptr_addr);
  438. }
  439. unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
  440. {
  441. int cmdline_length = 0;
  442. int n;
  443. char *dest;
  444. cmdline_length = strlen((const char*)cmdline);
  445. n = (cmdline_length + 4) & (~3);
  446. *ptr++ = (n / 4) + 2;
  447. *ptr++ = 0x54410009;
  448. dest = (char *) ptr;
  449. while ((*dest++ = *cmdline++));
  450. ptr += (n / 4);
  451. return ptr;
  452. }
  453. unsigned *atag_end(unsigned *ptr)
  454. {
  455. /* END */
  456. *ptr++ = 0;
  457. *ptr++ = 0;
  458. return ptr;
  459. }
  460. void generate_atags(unsigned *ptr, const char *cmdline,
  461. void *ramdisk, unsigned ramdisk_size)
  462. {
  463. ptr = atag_core(ptr);
  464. ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
  465. ptr = target_atag_mem(ptr);
  466. /* Skip NAND partition ATAGS for eMMC boot */
  467. if (!target_is_emmc_boot()){
  468. ptr = atag_ptable(&ptr);
  469. }
  470. ptr = atag_cmdline(ptr, cmdline);
  471. ptr = atag_end(ptr);
  472. }
  473. typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
  474. void boot_linux(void *kernel, unsigned *tags,
  475. const char *cmdline, unsigned machtype,
  476. void *ramdisk, unsigned ramdisk_size)
  477. {
  478. unsigned char *final_cmdline;
  479. #if DEVICE_TREE
  480. int ret = 0;
  481. #endif
  482. void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
  483. uint32_t tags_phys = PA((addr_t)tags);
  484. struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
  485. ramdisk = PA(ramdisk);
  486. final_cmdline = update_cmdline((const char*)cmdline);
  487. #if DEVICE_TREE
  488. dprintf(INFO, "Updating device tree: start\n");
  489. /* Update the Device Tree */
  490. ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
  491. if(ret)
  492. {
  493. dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
  494. ASSERT(0);
  495. }
  496. dprintf(INFO, "Updating device tree: done\n");
  497. #else
  498. /* Generating the Atags */
  499. generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
  500. #endif
  501. /* Perform target specific cleanup */
  502. target_uninit();
  503. /* Turn off splash screen if enabled */
  504. #if DISPLAY_SPLASH_SCREEN
  505. target_display_shutdown();
  506. #endif
  507. dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
  508. entry, ramdisk, ramdisk_size, tags_phys);
  509. enter_critical_section();
  510. /* do any platform specific cleanup before kernel entry */
  511. platform_uninit();
  512. arch_disable_cache(UCACHE);
  513. #if ARM_WITH_MMU
  514. arch_disable_mmu();
  515. #endif
  516. bs_set_timestamp(BS_KERNEL_ENTRY);
  517. if (IS_ARM64(kptr))
  518. /* Jump to a 64bit kernel */
  519. scm_elexec_call((paddr_t)kernel, tags_phys);
  520. else
  521. /* Jump to a 32bit kernel */
  522. entry(0, machtype, (unsigned*)tags_phys);
  523. }
  524. /* Function to check if the memory address range falls within the aboot
  525. * boundaries.
  526. * start: Start of the memory region
  527. * size: Size of the memory region
  528. */
  529. int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
  530. {
  531. /* Check for boundary conditions. */
  532. if ((UINT_MAX - start) < size)
  533. return -1;
  534. /* Check for memory overlap. */
  535. if ((start < MEMBASE) && ((start + size) <= MEMBASE))
  536. return 0;
  537. else if (start >= (MEMBASE + MEMSIZE))
  538. return 0;
  539. else
  540. return -1;
  541. }
  542. #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
  543. BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
  544. #if DEVICE_TREE
  545. BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
  546. #endif
  547. static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
  548. {
  549. int ret;
  550. #if IMAGE_VERIF_ALGO_SHA1
  551. uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
  552. #else
  553. uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
  554. #endif
  555. /* Assume device is rooted at this time. */
  556. device.is_tampered = 1;
  557. dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
  558. ret = image_verify((unsigned char *)bootimg_addr,
  559. (unsigned char *)(bootimg_addr + bootimg_size),
  560. bootimg_size,
  561. auth_algo);
  562. dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
  563. if (ret)
  564. {
  565. /* Authorized kernel */
  566. device.is_tampered = 0;
  567. auth_kernel_img = 1;
  568. }
  569. #if USE_PCOM_SECBOOT
  570. set_tamper_flag(device.is_tampered);
  571. #endif
  572. if(device.is_tampered)
  573. {
  574. write_device_info_mmc(&device);
  575. #ifdef TZ_TAMPER_FUSE
  576. set_tamper_fuse_cmd();
  577. #endif
  578. #ifdef ASSERT_ON_TAMPER
  579. dprintf(CRITICAL, "Device is tampered. Asserting..\n");
  580. ASSERT(0);
  581. #endif
  582. }
  583. }
  584. static bool check_format_bit()
  585. {
  586. bool ret = false;
  587. int index;
  588. uint64_t offset;
  589. struct boot_selection_info *in = NULL;
  590. char *buf = NULL;
  591. index = partition_get_index("bootselect");
  592. if (index == INVALID_PTN)
  593. {
  594. dprintf(INFO, "Unable to locate /bootselect partition\n");
  595. return ret;
  596. }
  597. offset = partition_get_offset(index);
  598. if(!offset)
  599. {
  600. dprintf(INFO, "partition /bootselect doesn't exist\n");
  601. return ret;
  602. }
  603. buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
  604. ASSERT(buf);
  605. if (mmc_read(offset, (unsigned int *)buf, page_size))
  606. {
  607. dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
  608. free(buf);
  609. return ret;
  610. }
  611. in = (struct boot_selection_info *) buf;
  612. if ((in->signature == BOOTSELECT_SIGNATURE) &&
  613. (in->version == BOOTSELECT_VERSION)) {
  614. if ((in->state_info & BOOTSELECT_FORMAT) &&
  615. !(in->state_info & BOOTSELECT_FACTORY))
  616. ret = true;
  617. } else {
  618. dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
  619. in->signature, in->version);
  620. ASSERT(0);
  621. }
  622. free(buf);
  623. return ret;
  624. }
  625. int boot_linux_from_mmc(void)
  626. {
  627. struct boot_img_hdr *hdr = (void*) buf;
  628. struct boot_img_hdr *uhdr;
  629. unsigned offset = 0;
  630. int rcode;
  631. unsigned long long ptn = 0;
  632. int index = INVALID_PTN;
  633. unsigned char *image_addr = 0;
  634. unsigned kernel_actual;
  635. unsigned ramdisk_actual;
  636. unsigned imagesize_actual;
  637. unsigned second_actual = 0;
  638. #if DEVICE_TREE
  639. struct dt_table *table;
  640. struct dt_entry dt_entry;
  641. unsigned dt_table_offset;
  642. uint32_t dt_actual;
  643. uint32_t dt_hdr_size;
  644. #endif
  645. BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
  646. struct kernel64_hdr *kptr = (void*) kbuf;
  647. if (check_format_bit())
  648. boot_into_recovery = 1;
  649. if (!boot_into_recovery) {
  650. memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
  651. rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
  652. if (rcode <= 0) {
  653. boot_into_ffbm = false;
  654. if (rcode < 0)
  655. dprintf(CRITICAL,"failed to get ffbm cookie");
  656. } else
  657. boot_into_ffbm = true;
  658. } else
  659. boot_into_ffbm = false;
  660. uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
  661. if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  662. dprintf(INFO, "Unified boot method!\n");
  663. hdr = uhdr;
  664. goto unified_boot;
  665. }
  666. if (!boot_into_recovery) {
  667. index = partition_get_index("boot");
  668. ptn = partition_get_offset(index);
  669. if(ptn == 0) {
  670. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  671. return -1;
  672. }
  673. }
  674. else {
  675. index = partition_get_index("recovery");
  676. ptn = partition_get_offset(index);
  677. if(ptn == 0) {
  678. dprintf(CRITICAL, "ERROR: No recovery partition found\n");
  679. return -1;
  680. }
  681. }
  682. if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
  683. dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
  684. return -1;
  685. }
  686. if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  687. dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  688. return -1;
  689. }
  690. if (hdr->page_size && (hdr->page_size != page_size)) {
  691. if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
  692. dprintf(CRITICAL, "ERROR: Invalid page size\n");
  693. return -1;
  694. }
  695. page_size = hdr->page_size;
  696. page_mask = page_size - 1;
  697. }
  698. /* Read the next page to get kernel Image header
  699. * which lives in the second page for arm64 targets.
  700. */
  701. if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
  702. dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
  703. return -1;
  704. }
  705. /*
  706. * Update the kernel/ramdisk/tags address if the boot image header
  707. * has default values, these default values come from mkbootimg when
  708. * the boot image is flashed using fastboot flash:raw
  709. */
  710. update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
  711. /* Get virtual addresses since the hdr saves physical addresses. */
  712. hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
  713. hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
  714. hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
  715. kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  716. ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  717. /* Check if the addresses in the header are valid. */
  718. if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
  719. check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
  720. {
  721. dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
  722. return -1;
  723. }
  724. #ifndef DEVICE_TREE
  725. if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
  726. {
  727. dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
  728. return -1;
  729. }
  730. #endif
  731. /* Authenticate Kernel */
  732. dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
  733. (int) target_use_signed_kernel(),
  734. device.is_unlocked,
  735. device.is_tampered);
  736. if(target_use_signed_kernel() && (!device.is_unlocked))
  737. {
  738. offset = 0;
  739. image_addr = (unsigned char *)target_get_scratch_address();
  740. #if DEVICE_TREE
  741. dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
  742. imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
  743. if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
  744. {
  745. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  746. return -1;
  747. }
  748. #else
  749. imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
  750. #endif
  751. dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
  752. bs_set_timestamp(BS_KERNEL_LOAD_START);
  753. if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
  754. {
  755. dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
  756. return -1;
  757. }
  758. /* Read image without signature */
  759. if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
  760. {
  761. dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
  762. return -1;
  763. }
  764. dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
  765. bs_set_timestamp(BS_KERNEL_LOAD_DONE);
  766. offset = imagesize_actual;
  767. if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
  768. {
  769. dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
  770. return -1;
  771. }
  772. /* Read signature */
  773. if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
  774. {
  775. dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
  776. return -1;
  777. }
  778. verify_signed_bootimg(image_addr, imagesize_actual);
  779. /* Move kernel, ramdisk and device tree to correct address */
  780. memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
  781. memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
  782. #if DEVICE_TREE
  783. if(hdr->dt_size) {
  784. dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
  785. table = (struct dt_table*) dt_table_offset;
  786. if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
  787. dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
  788. return -1;
  789. }
  790. /* Find index of device tree within device tree table */
  791. if(dev_tree_get_entry_info(table, &dt_entry) != 0){
  792. dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
  793. return -1;
  794. }
  795. /* Validate and Read device device tree in the "tags_add */
  796. if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
  797. {
  798. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  799. return -1;
  800. }
  801. memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
  802. } else {
  803. /*
  804. * If appended dev tree is found, update the atags with
  805. * memory address to the DTB appended location on RAM.
  806. * Else update with the atags address in the kernel header
  807. */
  808. void *dtb;
  809. dtb = dev_tree_appended((void*) hdr->kernel_addr,
  810. hdr->kernel_size,
  811. (void *)hdr->tags_addr);
  812. if (!dtb) {
  813. dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
  814. return -1;
  815. }
  816. }
  817. #endif
  818. }
  819. else
  820. {
  821. second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
  822. dprintf(INFO, "Loading boot image (%d): start\n",
  823. kernel_actual + ramdisk_actual);
  824. bs_set_timestamp(BS_KERNEL_LOAD_START);
  825. offset = page_size;
  826. /* Load kernel */
  827. if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
  828. dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
  829. return -1;
  830. }
  831. offset += kernel_actual;
  832. /* Load ramdisk */
  833. if(ramdisk_actual != 0)
  834. {
  835. if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
  836. dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
  837. return -1;
  838. }
  839. }
  840. offset += ramdisk_actual;
  841. dprintf(INFO, "Loading boot image (%d): done\n",
  842. kernel_actual + ramdisk_actual);
  843. bs_set_timestamp(BS_KERNEL_LOAD_DONE);
  844. if(hdr->second_size != 0) {
  845. offset += second_actual;
  846. /* Second image loading not implemented. */
  847. ASSERT(0);
  848. }
  849. #if DEVICE_TREE
  850. if(hdr->dt_size != 0) {
  851. /* Read the first page of device tree table into buffer */
  852. if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
  853. dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
  854. return -1;
  855. }
  856. table = (struct dt_table*) dt_buf;
  857. if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
  858. dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
  859. return -1;
  860. }
  861. table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
  862. if (!table)
  863. return -1;
  864. /* Read the entire device tree table into buffer */
  865. if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
  866. dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
  867. return -1;
  868. }
  869. /* Find index of device tree within device tree table */
  870. if(dev_tree_get_entry_info(table, &dt_entry) != 0){
  871. dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
  872. return -1;
  873. }
  874. /* Validate and Read device device tree in the "tags_add */
  875. if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
  876. {
  877. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  878. return -1;
  879. }
  880. if(mmc_read(ptn + offset + dt_entry.offset,
  881. (void *)hdr->tags_addr, dt_entry.size)) {
  882. dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
  883. return -1;
  884. }
  885. #ifdef TZ_SAVE_KERNEL_HASH
  886. aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
  887. hdr->ramdisk_addr, ramdisk_actual,
  888. ptn, offset, hdr->dt_size);
  889. #endif /* TZ_SAVE_KERNEL_HASH */
  890. } else {
  891. /* Validate the tags_addr */
  892. if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
  893. {
  894. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  895. return -1;
  896. }
  897. /*
  898. * If appended dev tree is found, update the atags with
  899. * memory address to the DTB appended location on RAM.
  900. * Else update with the atags address in the kernel header
  901. */
  902. void *dtb;
  903. dtb = dev_tree_appended((void*) hdr->kernel_addr,
  904. kernel_actual,
  905. (void *)hdr->tags_addr);
  906. if (!dtb) {
  907. dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
  908. return -1;
  909. }
  910. }
  911. #endif
  912. }
  913. if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
  914. target_load_ssd_keystore();
  915. unified_boot:
  916. boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
  917. (const char *)hdr->cmdline, board_machtype(),
  918. (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
  919. return 0;
  920. }
  921. int boot_linux_from_flash(void)
  922. {
  923. struct boot_img_hdr *hdr = (void*) buf;
  924. struct ptentry *ptn;
  925. struct ptable *ptable;
  926. unsigned offset = 0;
  927. unsigned char *image_addr = 0;
  928. unsigned kernel_actual;
  929. unsigned ramdisk_actual;
  930. unsigned imagesize_actual;
  931. unsigned second_actual;
  932. #if DEVICE_TREE
  933. struct dt_table *table;
  934. struct dt_entry dt_entry;
  935. uint32_t dt_actual;
  936. uint32_t dt_hdr_size;
  937. #endif
  938. if (target_is_emmc_boot()) {
  939. hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
  940. if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  941. dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  942. return -1;
  943. }
  944. goto continue_boot;
  945. }
  946. ptable = flash_get_ptable();
  947. if (ptable == NULL) {
  948. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  949. return -1;
  950. }
  951. if(!boot_into_recovery)
  952. {
  953. ptn = ptable_find(ptable, "boot");
  954. if (ptn == NULL) {
  955. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  956. return -1;
  957. }
  958. }
  959. else
  960. {
  961. ptn = ptable_find(ptable, "recovery");
  962. if (ptn == NULL) {
  963. dprintf(CRITICAL, "ERROR: No recovery partition found\n");
  964. return -1;
  965. }
  966. }
  967. if (flash_read(ptn, offset, buf, page_size)) {
  968. dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
  969. return -1;
  970. }
  971. if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  972. dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
  973. return -1;
  974. }
  975. if (hdr->page_size != page_size) {
  976. dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
  977. return -1;
  978. }
  979. /*
  980. * Update the kernel/ramdisk/tags address if the boot image header
  981. * has default values, these default values come from mkbootimg when
  982. * the boot image is flashed using fastboot flash:raw
  983. */
  984. update_ker_tags_rdisk_addr(hdr, false);
  985. /* Get virtual addresses since the hdr saves physical addresses. */
  986. hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
  987. hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
  988. hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
  989. kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  990. ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  991. /* Check if the addresses in the header are valid. */
  992. if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
  993. check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
  994. {
  995. dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
  996. return -1;
  997. }
  998. #ifndef DEVICE_TREE
  999. if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
  1000. {
  1001. dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
  1002. return -1;
  1003. }
  1004. #endif
  1005. /* Authenticate Kernel */
  1006. if(target_use_signed_kernel() && (!device.is_unlocked))
  1007. {
  1008. image_addr = (unsigned char *)target_get_scratch_address();
  1009. offset = 0;
  1010. #if DEVICE_TREE
  1011. dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
  1012. imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
  1013. if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
  1014. {
  1015. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  1016. return -1;
  1017. }
  1018. #else
  1019. imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
  1020. #endif
  1021. dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
  1022. bs_set_timestamp(BS_KERNEL_LOAD_START);
  1023. /* Read image without signature */
  1024. if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
  1025. {
  1026. dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
  1027. return -1;
  1028. }
  1029. dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
  1030. bs_set_timestamp(BS_KERNEL_LOAD_DONE);
  1031. offset = imagesize_actual;
  1032. /* Read signature */
  1033. if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
  1034. {
  1035. dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
  1036. return -1;
  1037. }
  1038. verify_signed_bootimg(image_addr, imagesize_actual);
  1039. /* Move kernel and ramdisk to correct address */
  1040. memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
  1041. memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
  1042. #if DEVICE_TREE
  1043. /* Validate and Read device device tree in the "tags_add */
  1044. if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
  1045. {
  1046. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  1047. return -1;
  1048. }
  1049. memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
  1050. #endif
  1051. /* Make sure everything from scratch address is read before next step!*/
  1052. if(device.is_tampered)
  1053. {
  1054. write_device_info_flash(&device);
  1055. }
  1056. #if USE_PCOM_SECBOOT
  1057. set_tamper_flag(device.is_tampered);
  1058. #endif
  1059. }
  1060. else
  1061. {
  1062. offset = page_size;
  1063. kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  1064. ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  1065. second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
  1066. dprintf(INFO, "Loading boot image (%d): start\n",
  1067. kernel_actual + ramdisk_actual);
  1068. bs_set_timestamp(BS_KERNEL_LOAD_START);
  1069. if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
  1070. dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
  1071. return -1;
  1072. }
  1073. offset += kernel_actual;
  1074. if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
  1075. dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
  1076. return -1;
  1077. }
  1078. offset += ramdisk_actual;
  1079. dprintf(INFO, "Loading boot image (%d): done\n",
  1080. kernel_actual + ramdisk_actual);
  1081. bs_set_timestamp(BS_KERNEL_LOAD_DONE);
  1082. if(hdr->second_size != 0) {
  1083. offset += second_actual;
  1084. /* Second image loading not implemented. */
  1085. ASSERT(0);
  1086. }
  1087. #if DEVICE_TREE
  1088. if(hdr->dt_size != 0) {
  1089. /* Read the device tree table into buffer */
  1090. if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
  1091. dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
  1092. return -1;
  1093. }
  1094. table = (struct dt_table*) dt_buf;
  1095. if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
  1096. dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
  1097. return -1;
  1098. }
  1099. table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
  1100. if (!table)
  1101. return -1;
  1102. /* Read the entire device tree table into buffer */
  1103. if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
  1104. dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
  1105. return -1;
  1106. }
  1107. /* Find index of device tree within device tree table */
  1108. if(dev_tree_get_entry_info(table, &dt_entry) != 0){
  1109. dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
  1110. return -1;
  1111. }
  1112. /* Validate and Read device device tree in the "tags_add */
  1113. if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
  1114. {
  1115. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  1116. return -1;
  1117. }
  1118. /* Read device device tree in the "tags_add */
  1119. if(flash_read(ptn, offset + dt_entry.offset,
  1120. (void *)hdr->tags_addr, dt_entry.size)) {
  1121. dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
  1122. return -1;
  1123. }
  1124. }
  1125. #endif
  1126. }
  1127. continue_boot:
  1128. /* TODO: create/pass atags to kernel */
  1129. boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
  1130. (const char *)hdr->cmdline, board_machtype(),
  1131. (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
  1132. return 0;
  1133. }
  1134. BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
  1135. void write_device_info_mmc(device_info *dev)
  1136. {
  1137. struct device_info *info = (void*) info_buf;
  1138. unsigned long long ptn = 0;
  1139. unsigned long long size;
  1140. int index = INVALID_PTN;
  1141. uint32_t blocksize;
  1142. uint8_t lun = 0;
  1143. index = partition_get_index("aboot");
  1144. ptn = partition_get_offset(index);
  1145. if(ptn == 0)
  1146. {
  1147. return;
  1148. }
  1149. lun = partition_get_lun(index);
  1150. mmc_set_lun(lun);
  1151. size = partition_get_size(index);
  1152. memcpy(info, dev, sizeof(device_info));
  1153. blocksize = mmc_get_device_blocksize();
  1154. if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
  1155. {
  1156. dprintf(CRITICAL, "ERROR: Cannot write device info\n");
  1157. return;
  1158. }
  1159. }
  1160. void read_device_info_mmc(device_info *dev)
  1161. {
  1162. struct device_info *info = (void*) info_buf;
  1163. unsigned long long ptn = 0;
  1164. unsigned long long size;
  1165. int index = INVALID_PTN;
  1166. uint32_t blocksize;
  1167. index = partition_get_index("aboot");
  1168. ptn = partition_get_offset(index);
  1169. if(ptn == 0)
  1170. {
  1171. return;
  1172. }
  1173. size = partition_get_size(index);
  1174. blocksize = mmc_get_device_blocksize();
  1175. if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
  1176. {
  1177. dprintf(CRITICAL, "ERROR: Cannot read device info\n");
  1178. return;
  1179. }
  1180. if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
  1181. {
  1182. memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
  1183. info->is_unlocked = 0;
  1184. info->is_tampered = 0;
  1185. info->charger_screen_enabled = 0;
  1186. write_device_info_mmc(info);
  1187. }
  1188. memcpy(dev, info, sizeof(device_info));
  1189. }
  1190. void write_device_info_flash(device_info *dev)
  1191. {
  1192. struct device_info *info = (void *) info_buf;
  1193. struct ptentry *ptn;
  1194. struct ptable *ptable;
  1195. ptable = flash_get_ptable();
  1196. if (ptable == NULL)
  1197. {
  1198. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  1199. return;
  1200. }
  1201. ptn = ptable_find(ptable, "devinfo");
  1202. if (ptn == NULL)
  1203. {
  1204. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  1205. return;
  1206. }
  1207. memcpy(info, dev, sizeof(device_info));
  1208. if (flash_write(ptn, 0, (void *)info_buf, page_size))
  1209. {
  1210. dprintf(CRITICAL, "ERROR: Cannot write device info\n");
  1211. return;
  1212. }
  1213. }
  1214. void read_device_info_flash(device_info *dev)
  1215. {
  1216. struct device_info *info = (void*) info_buf;
  1217. struct ptentry *ptn;
  1218. struct ptable *ptable;
  1219. ptable = flash_get_ptable();
  1220. if (ptable == NULL)
  1221. {
  1222. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  1223. return;
  1224. }
  1225. ptn = ptable_find(ptable, "devinfo");
  1226. if (ptn == NULL)
  1227. {
  1228. dprintf(CRITICAL, "ERROR: No boot partition found\n");
  1229. return;
  1230. }
  1231. if (flash_read(ptn, 0, (void *)info_buf, page_size))
  1232. {
  1233. dprintf(CRITICAL, "ERROR: Cannot write device info\n");
  1234. return;
  1235. }
  1236. if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
  1237. {
  1238. memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
  1239. info->is_unlocked = 0;
  1240. info->is_tampered = 0;
  1241. write_device_info_flash(info);
  1242. }
  1243. memcpy(dev, info, sizeof(device_info));
  1244. }
  1245. void write_device_info(device_info *dev)
  1246. {
  1247. if(target_is_emmc_boot())
  1248. {
  1249. write_device_info_mmc(dev);
  1250. }
  1251. else
  1252. {
  1253. write_device_info_flash(dev);
  1254. }
  1255. }
  1256. void read_device_info(device_info *dev)
  1257. {
  1258. if(target_is_emmc_boot())
  1259. {
  1260. read_device_info_mmc(dev);
  1261. }
  1262. else
  1263. {
  1264. read_device_info_flash(dev);
  1265. }
  1266. }
  1267. void reset_device_info()
  1268. {
  1269. dprintf(ALWAYS, "reset_device_info called.");
  1270. device.is_tampered = 0;
  1271. write_device_info(&device);
  1272. }
  1273. void set_device_root()
  1274. {
  1275. dprintf(ALWAYS, "set_device_root called.");
  1276. device.is_tampered = 1;
  1277. write_device_info(&device);
  1278. }
  1279. #if DEVICE_TREE
  1280. int copy_dtb(uint8_t *boot_image_start)
  1281. {
  1282. uint32 dt_image_offset = 0;
  1283. uint32_t n;
  1284. struct dt_table *table;
  1285. struct dt_entry dt_entry;
  1286. uint32_t dt_hdr_size;
  1287. struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
  1288. if(hdr->dt_size != 0) {
  1289. /* add kernel offset */
  1290. dt_image_offset += page_size;
  1291. n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  1292. dt_image_offset += n;
  1293. /* add ramdisk offset */
  1294. n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  1295. dt_image_offset += n;
  1296. /* add second offset */
  1297. if(hdr->second_size != 0) {
  1298. n = ROUND_TO_PAGE(hdr->second_size, page_mask);
  1299. dt_image_offset += n;
  1300. }
  1301. /* offset now point to start of dt.img */
  1302. table = (struct dt_table*)(boot_image_start + dt_image_offset);
  1303. if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
  1304. dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
  1305. return -1;
  1306. }
  1307. /* Find index of device tree within device tree table */
  1308. if(dev_tree_get_entry_info(table, &dt_entry) != 0){
  1309. dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
  1310. return -1;
  1311. }
  1312. /* Validate and Read device device tree in the "tags_add */
  1313. if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
  1314. {
  1315. dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
  1316. return -1;
  1317. }
  1318. /* Read device device tree in the "tags_add */
  1319. memmove((void*) hdr->tags_addr,
  1320. boot_image_start + dt_image_offset + dt_entry.offset,
  1321. dt_entry.size);
  1322. } else
  1323. return -1;
  1324. /* Everything looks fine. Return success. */
  1325. return 0;
  1326. }
  1327. #endif
  1328. void cmd_boot(const char *arg, void *data, unsigned sz)
  1329. {
  1330. unsigned kernel_actual;
  1331. unsigned ramdisk_actual;
  1332. uint32_t image_actual;
  1333. uint32_t dt_actual = 0;
  1334. uint32_t sig_actual = SIGNATURE_SIZE;
  1335. struct boot_img_hdr *hdr;
  1336. struct kernel64_hdr *kptr;
  1337. char *ptr = ((char*) data);
  1338. int ret = 0;
  1339. uint8_t dtb_copied = 0;
  1340. if (sz < sizeof(hdr)) {
  1341. fastboot_fail("invalid bootimage header");
  1342. return;
  1343. }
  1344. hdr = (struct boot_img_hdr *)data;
  1345. /* ensure commandline is terminated */
  1346. hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
  1347. if(target_is_emmc_boot() && hdr->page_size) {
  1348. page_size = hdr->page_size;
  1349. page_mask = page_size - 1;
  1350. }
  1351. kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
  1352. ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
  1353. #if DEVICE_TREE
  1354. dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
  1355. #endif
  1356. image_actual = ADD_OF(page_size, kernel_actual);
  1357. image_actual = ADD_OF(image_actual, ramdisk_actual);
  1358. image_actual = ADD_OF(image_actual, dt_actual);
  1359. if (target_use_signed_kernel() && (!device.is_unlocked))
  1360. image_actual = ADD_OF(image_actual, sig_actual);
  1361. /* sz should have atleast raw boot image */
  1362. if (image_actual > sz) {
  1363. fastboot_fail("bootimage: incomplete or not signed");
  1364. return;
  1365. }
  1366. /* Verify the boot image
  1367. * device & page_size are initialized in aboot_init
  1368. */
  1369. if (target_use_signed_kernel() && (!device.is_unlocked))
  1370. /* Pass size excluding signature size, otherwise we would try to
  1371. * access signature beyond its length
  1372. */
  1373. verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
  1374. /*
  1375. * Update the kernel/ramdisk/tags address if the boot image header
  1376. * has default values, these default values come from mkbootimg when
  1377. * the boot image is flashed using fastboot flash:raw
  1378. */
  1379. kptr = (struct kernel64_hdr*)((char*) data + page_size);
  1380. update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
  1381. /* Get virtual addresses since the hdr saves physical addresses. */
  1382. hdr->kernel_addr = VA(hdr->kernel_addr);
  1383. hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
  1384. hdr->tags_addr = VA(hdr->tags_addr);
  1385. /* Check if the addresses in the header are valid. */
  1386. if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
  1387. check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
  1388. {
  1389. dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
  1390. return;
  1391. }
  1392. #if DEVICE_TREE
  1393. /* find correct dtb and copy it to right location */
  1394. ret = copy_dtb(data);
  1395. dtb_copied = !ret ? 1 : 0;
  1396. #else
  1397. if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
  1398. {
  1399. dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
  1400. return;
  1401. }
  1402. #endif
  1403. /* Load ramdisk & kernel */
  1404. memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
  1405. memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
  1406. #if DEVICE_TREE
  1407. /*
  1408. * If dtb is not found look for appended DTB in the kernel.
  1409. * If appended dev tree is found, update the atags with
  1410. * memory address to the DTB appended location on RAM.
  1411. * Else update with the atags address in the kernel header
  1412. */
  1413. if (!dtb_copied) {
  1414. void *dtb;
  1415. dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
  1416. (void *)hdr->tags_addr);
  1417. if (!dtb) {
  1418. fastboot_fail("dtb not found");
  1419. return;
  1420. }
  1421. }
  1422. #endif
  1423. #ifndef DEVICE_TREE
  1424. if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
  1425. {
  1426. dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
  1427. return;
  1428. }
  1429. #endif
  1430. fastboot_okay("");
  1431. fastboot_stop();
  1432. boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
  1433. (const char*) hdr->cmdline, board_machtype(),
  1434. (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
  1435. }
  1436. void cmd_erase(const char *arg, void *data, unsigned sz)
  1437. {
  1438. struct ptentry *ptn;
  1439. struct ptable *ptable;
  1440. ptable = flash_get_ptable();
  1441. if (ptable == NULL) {
  1442. fastboot_fail("partition table doesn't exist");
  1443. return;
  1444. }
  1445. ptn = ptable_find(ptable, arg);
  1446. if (ptn == NULL) {
  1447. fastboot_fail("unknown partition name");
  1448. return;
  1449. }
  1450. if (flash_erase(ptn)) {
  1451. fastboot_fail("failed to erase partition");
  1452. return;
  1453. }
  1454. fastboot_okay("");
  1455. }
  1456. void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
  1457. {
  1458. BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
  1459. unsigned long long ptn = 0;
  1460. unsigned long long size = 0;
  1461. int index = INVALID_PTN;
  1462. uint8_t lun = 0;
  1463. index = partition_get_index(arg);
  1464. ptn = partition_get_offset(index);
  1465. size = partition_get_size(index);
  1466. if(ptn == 0) {
  1467. fastboot_fail("Partition table doesn't exist\n");
  1468. return;
  1469. }
  1470. lun = partition_get_lun(index);
  1471. mmc_set_lun(lun);
  1472. #if MMC_SDHCI_SUPPORT
  1473. if (mmc_erase_card(ptn, size)) {
  1474. fastboot_fail("failed to erase partition\n");
  1475. return;
  1476. }
  1477. #else
  1478. size = partition_get_size(index);
  1479. if (size > DEFAULT_ERASE_SIZE)
  1480. size = DEFAULT_ERASE_SIZE;
  1481. /* Simple inefficient version of erase. Just writing
  1482. 0 in first several blocks */
  1483. if (mmc_write(ptn , size, (unsigned int *)out)) {
  1484. fastboot_fail("failed to erase partition");
  1485. return;
  1486. }
  1487. #endif
  1488. fastboot_okay("");
  1489. }
  1490. void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
  1491. {
  1492. unsigned long long ptn = 0;
  1493. unsigned long long size = 0;
  1494. int index = INVALID_PTN;
  1495. char *token = NULL;
  1496. char *pname = NULL;
  1497. uint8_t lun = 0;
  1498. bool lun_set = false;
  1499. token = strtok(arg, ":");
  1500. pname = token;
  1501. token = strtok(NULL, ":");
  1502. if(token)
  1503. {
  1504. lun = atoi(token);
  1505. mmc_set_lun(lun);
  1506. lun_set = true;
  1507. }
  1508. if (pname)
  1509. {
  1510. if (!strcmp(pname, "partition"))
  1511. {
  1512. dprintf(INFO, "Attempt to write partition image.\n");
  1513. if (write_partition(sz, (unsigned char *) data)) {
  1514. fastboot_fail("failed to write partition");
  1515. return;
  1516. }
  1517. }
  1518. else
  1519. {
  1520. index = partition_get_index(pname);
  1521. ptn = partition_get_offset(index);
  1522. if(ptn == 0) {
  1523. fastboot_fail("partition table doesn't exist");
  1524. return;
  1525. }
  1526. if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
  1527. if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  1528. fastboot_fail("image is not a boot image");
  1529. return;
  1530. }
  1531. }
  1532. if(!lun_set)
  1533. {
  1534. lun = partition_get_lun(index);
  1535. mmc_set_lun(lun);
  1536. }
  1537. size = partition_get_size(index);
  1538. if (ROUND_TO_PAGE(sz,511) > size) {
  1539. fastboot_fail("size too large");
  1540. return;
  1541. }
  1542. else if (mmc_write(ptn , sz, (unsigned int *)data)) {
  1543. fastboot_fail("flash write failure");
  1544. return;
  1545. }
  1546. }
  1547. }
  1548. fastboot_okay("");
  1549. return;
  1550. }
  1551. void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
  1552. {
  1553. unsigned int chunk;
  1554. unsigned int chunk_data_sz;
  1555. uint32_t *fill_buf = NULL;
  1556. uint32_t fill_val;
  1557. uint32_t chunk_blk_cnt = 0;
  1558. sparse_header_t *sparse_header;
  1559. chunk_header_t *chunk_header;
  1560. uint32_t total_blocks = 0;
  1561. unsigned long long ptn = 0;
  1562. unsigned long long size = 0;
  1563. int index = INVALID_PTN;
  1564. int i;
  1565. uint8_t lun = 0;
  1566. index = partition_get_index(arg);
  1567. ptn = partition_get_offset(index);
  1568. if(ptn == 0) {
  1569. fastboot_fail("partition table doesn't exist");
  1570. return;
  1571. }
  1572. size = partition_get_size(index);
  1573. if (ROUND_TO_PAGE(sz,511) > size) {
  1574. fastboot_fail("size too large");
  1575. return;
  1576. }
  1577. lun = partition_get_lun(index);
  1578. mmc_set_lun(lun);
  1579. /* Read and skip over sparse image header */
  1580. sparse_header = (sparse_header_t *) data;
  1581. if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
  1582. fastboot_fail("size too large");
  1583. return;
  1584. }
  1585. data += sparse_header->file_hdr_sz;
  1586. if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
  1587. {
  1588. /* Skip the remaining bytes in a header that is longer than
  1589. * we expected.
  1590. */
  1591. data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
  1592. }
  1593. dprintf (SPEW, "=== Sparse Image Header ===\n");
  1594. dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
  1595. dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
  1596. dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
  1597. dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  1598. dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  1599. dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
  1600. dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
  1601. dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
  1602. /* Start processing chunks */
  1603. for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
  1604. {
  1605. /* Read and skip over chunk header */
  1606. chunk_header = (chunk_header_t *) data;
  1607. data += sizeof(chunk_header_t);
  1608. dprintf (SPEW, "=== Chunk Header ===\n");
  1609. dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
  1610. dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
  1611. dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
  1612. if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
  1613. {
  1614. /* Skip the remaining bytes in a header that is longer than
  1615. * we expected.
  1616. */
  1617. data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
  1618. }
  1619. chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
  1620. switch (chunk_header->chunk_type)
  1621. {
  1622. case CHUNK_TYPE_RAW:
  1623. if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
  1624. chunk_data_sz))
  1625. {
  1626. fastboot_fail("Bogus chunk size for chunk type Raw");
  1627. return;
  1628. }
  1629. if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
  1630. chunk_data_sz,
  1631. (unsigned int*)data))
  1632. {
  1633. fastboot_fail("flash write failure");
  1634. return;
  1635. }
  1636. total_blocks += chunk_header->chunk_sz;
  1637. data += chunk_data_sz;
  1638. break;
  1639. case CHUNK_TYPE_FILL:
  1640. if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
  1641. sizeof(uint32_t)))
  1642. {
  1643. fastboot_fail("Bogus chunk size for chunk type FILL");
  1644. return;
  1645. }
  1646. fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
  1647. if (!fill_buf)
  1648. {
  1649. fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
  1650. return;
  1651. }
  1652. fill_val = *(uint32_t *)data;
  1653. data = (char *) data + sizeof(uint32_t);
  1654. chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
  1655. for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
  1656. {
  1657. fill_buf[i] = fill_val;
  1658. }
  1659. for (i = 0; i < chunk_blk_cnt; i++)
  1660. {
  1661. if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
  1662. sparse_header->blk_sz,
  1663. fill_buf))
  1664. {
  1665. fastboot_fail("flash write failure");
  1666. free(fill_buf);
  1667. return;
  1668. }
  1669. total_blocks++;
  1670. }
  1671. free(fill_buf);
  1672. break;
  1673. case CHUNK_TYPE_DONT_CARE:
  1674. total_blocks += chunk_header->chunk_sz;
  1675. break;
  1676. case CHUNK_TYPE_CRC:
  1677. if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
  1678. {
  1679. fastboot_fail("Bogus chunk size for chunk type Dont Care");
  1680. return;
  1681. }
  1682. total_blocks += chunk_header->chunk_sz;
  1683. data += chunk_data_sz;
  1684. break;
  1685. default:
  1686. dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
  1687. fastboot_fail("Unknown chunk type");
  1688. return;
  1689. }
  1690. }
  1691. dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
  1692. total_blocks, sparse_header->total_blks);
  1693. if(total_blocks != sparse_header->total_blks)
  1694. {
  1695. fastboot_fail("sparse image write failure");
  1696. }
  1697. fastboot_okay("");
  1698. return;
  1699. }
  1700. void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
  1701. {
  1702. sparse_header_t *sparse_header;
  1703. /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
  1704. unsigned int *magic_number = (unsigned int *) data;
  1705. #ifdef SSD_ENABLE
  1706. int ret=0;
  1707. uint32 major_version=0;
  1708. uint32 minor_version=0;
  1709. ret = scm_svc_version(&major_version,&minor_version);
  1710. if(!ret)
  1711. {
  1712. if(major_version >= 2)
  1713. {
  1714. if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
  1715. {
  1716. ret = encrypt_scm((uint32 **) &data, &sz);
  1717. if (ret != 0) {
  1718. dprintf(CRITICAL, "ERROR: Encryption Failure\n");
  1719. return;
  1720. }
  1721. /* Protect only for SSD */
  1722. if (!strcmp(arg, "ssd")) {
  1723. ret = scm_protect_keystore((uint32 *) data, sz);
  1724. if (ret != 0) {
  1725. dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
  1726. return;
  1727. }
  1728. }
  1729. }
  1730. else
  1731. {
  1732. ret = decrypt_scm_v2((uint32 **) &data, &sz);
  1733. if(ret != 0)
  1734. {
  1735. dprintf(CRITICAL,"ERROR: Decryption Failure\n");
  1736. return;
  1737. }
  1738. }
  1739. }
  1740. else
  1741. {
  1742. if (magic_number[0] == DECRYPT_MAGIC_0 &&
  1743. magic_number[1] == DECRYPT_MAGIC_1)
  1744. {
  1745. ret = decrypt_scm((uint32 **) &data, &sz);
  1746. if (ret != 0) {
  1747. dprintf(CRITICAL, "ERROR: Invalid secure image\n");
  1748. return;
  1749. }
  1750. }
  1751. else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
  1752. magic_number[1] == ENCRYPT_MAGIC_1)
  1753. {
  1754. ret = encrypt_scm((uint32 **) &data, &sz);
  1755. if (ret != 0) {
  1756. dprintf(CRITICAL, "ERROR: Encryption Failure\n");
  1757. return;
  1758. }
  1759. }
  1760. }
  1761. }
  1762. else
  1763. {
  1764. dprintf(CRITICAL,"INVALID SVC Version\n");
  1765. return;
  1766. }
  1767. #endif /* SSD_ENABLE */
  1768. sparse_header = (sparse_header_t *) data;
  1769. if (sparse_header->magic != SPARSE_HEADER_MAGIC)
  1770. cmd_flash_mmc_img(arg, data, sz);
  1771. else
  1772. cmd_flash_mmc_sparse_img(arg, data, sz);
  1773. return;
  1774. }
  1775. void cmd_flash(const char *arg, void *data, unsigned sz)
  1776. {
  1777. struct ptentry *ptn;
  1778. struct ptable *ptable;
  1779. unsigned extra = 0;
  1780. ptable = flash_get_ptable();
  1781. if (ptable == NULL) {
  1782. fastboot_fail("partition table doesn't exist");
  1783. return;
  1784. }
  1785. ptn = ptable_find(ptable, arg);
  1786. if (ptn == NULL) {
  1787. fastboot_fail("unknown partition name");
  1788. return;
  1789. }
  1790. if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
  1791. if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  1792. fastboot_fail("image is not a boot image");
  1793. return;
  1794. }
  1795. }
  1796. if (!strcmp(ptn->name, "system")
  1797. || !strcmp(ptn->name, "userdata")
  1798. || !strcmp(ptn->name, "persist")
  1799. || !strcmp(ptn->name, "recoveryfs")
  1800. || !strcmp(ptn->name, "modem"))
  1801. {
  1802. if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
  1803. extra = 1;
  1804. else
  1805. extra = 0;
  1806. }
  1807. else
  1808. sz = ROUND_TO_PAGE(sz, page_mask);
  1809. dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
  1810. if (flash_write(ptn, extra, data, sz)) {
  1811. fastboot_fail("flash write failure");
  1812. return;
  1813. }
  1814. dprintf(INFO, "partition '%s' updated\n", ptn->name);
  1815. fastboot_okay("");
  1816. }
  1817. void cmd_continue(const char *arg, void *data, unsigned sz)
  1818. {
  1819. fastboot_okay("");
  1820. fastboot_stop();
  1821. if (target_is_emmc_boot())
  1822. {
  1823. boot_linux_from_mmc();
  1824. }
  1825. else
  1826. {
  1827. boot_linux_from_flash();
  1828. }
  1829. }
  1830. void cmd_reboot(const char *arg, void *data, unsigned sz)
  1831. {
  1832. dprintf(INFO, "rebooting the device\n");
  1833. fastboot_okay("");
  1834. reboot_device(0);
  1835. }
  1836. void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
  1837. {
  1838. dprintf(INFO, "rebooting the device\n");
  1839. fastboot_okay("");
  1840. reboot_device(FASTBOOT_MODE);
  1841. }
  1842. void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
  1843. {
  1844. dprintf(INFO, "Enabling charger screen check\n");
  1845. device.charger_screen_enabled = 1;
  1846. write_device_info(&device);
  1847. fastboot_okay("");
  1848. }
  1849. void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
  1850. {
  1851. dprintf(INFO, "Disabling charger screen check\n");
  1852. device.charger_screen_enabled = 0;
  1853. write_device_info(&device);
  1854. fastboot_okay("");
  1855. }
  1856. void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
  1857. {
  1858. dprintf(INFO, "Selecting display panel %s\n", arg);
  1859. if (arg)
  1860. strlcpy(device.display_panel, arg,
  1861. sizeof(device.display_panel));
  1862. write_device_info(&device);
  1863. fastboot_okay("");
  1864. }
  1865. void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
  1866. {
  1867. if(!device.is_unlocked)
  1868. {
  1869. device.is_unlocked = 1;
  1870. write_device_info(&device);
  1871. }
  1872. fastboot_okay("");
  1873. }
  1874. void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
  1875. {
  1876. char response[128];
  1877. snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
  1878. fastboot_info(response);
  1879. snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
  1880. fastboot_info(response);
  1881. snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
  1882. fastboot_info(response);
  1883. snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
  1884. fastboot_info(response);
  1885. fastboot_okay("");
  1886. }
  1887. void cmd_preflash(const char *arg, void *data, unsigned sz)
  1888. {
  1889. fastboot_okay("");
  1890. }
  1891. static struct fbimage logo_header = {0};
  1892. struct fbimage* splash_screen_flash();
  1893. int splash_screen_check_header(struct fbimage *logo)
  1894. {
  1895. if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
  1896. return -1;
  1897. if (logo->header.width == 0 || logo->header.height == 0)
  1898. return -1;
  1899. return 0;
  1900. }
  1901. struct fbimage* splash_screen_flash()
  1902. {
  1903. struct ptentry *ptn;
  1904. struct ptable *ptable;
  1905. struct fbcon_config *fb_display = NULL;
  1906. struct fbimage *logo = &logo_header;
  1907. ptable = flash_get_ptable();
  1908. if (ptable == NULL) {
  1909. dprintf(CRITICAL, "ERROR: Partition table not found\n");
  1910. return NULL;
  1911. }
  1912. ptn = ptable_find(ptable, "splash");
  1913. if (ptn == NULL) {
  1914. dprintf(CRITICAL, "ERROR: splash Partition not found\n");
  1915. return NULL;
  1916. }
  1917. if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
  1918. dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
  1919. return NULL;
  1920. }
  1921. if (splash_screen_check_header(logo)) {
  1922. dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
  1923. return NULL;
  1924. }
  1925. fb_display = fbcon_display();
  1926. if (fb_display) {
  1927. uint8_t *base = (uint8_t *) fb_display->base;
  1928. if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
  1929. base += LOGO_IMG_OFFSET;
  1930. }
  1931. if (flash_read(ptn + sizeof(logo->header), 0,
  1932. base,
  1933. ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
  1934. fbcon_clear();
  1935. dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
  1936. return NULL;
  1937. }
  1938. logo->image = base;
  1939. }
  1940. return logo;
  1941. }
  1942. struct fbimage* splash_screen_mmc()
  1943. {
  1944. int index = INVALID_PTN;
  1945. unsigned long long ptn = 0;
  1946. struct fbcon_config *fb_display = NULL;
  1947. struct fbimage *logo = &logo_header;
  1948. index = partition_get_index("splash");
  1949. if (index == 0) {
  1950. dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
  1951. return NULL;
  1952. }
  1953. ptn = partition_get_offset(index);
  1954. if (ptn == 0) {
  1955. dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
  1956. return NULL;
  1957. }
  1958. if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
  1959. dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
  1960. return NULL;
  1961. }
  1962. if (splash_screen_check_header(logo)) {
  1963. dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
  1964. return NULL;
  1965. }
  1966. fb_display = fbcon_display();
  1967. if (fb_display) {
  1968. uint8_t *base = (uint8_t *) fb_display->base;
  1969. if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
  1970. base += LOGO_IMG_OFFSET;
  1971. if (mmc_read(ptn + sizeof(logo->header),
  1972. base,
  1973. ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
  1974. fbcon_clear();
  1975. dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
  1976. return NULL;
  1977. }
  1978. logo->image = base;
  1979. }
  1980. return logo;
  1981. }
  1982. struct fbimage* fetch_image_from_partition()
  1983. {
  1984. if (target_is_emmc_boot()) {
  1985. return splash_screen_mmc();
  1986. } else {
  1987. return splash_screen_flash();
  1988. }
  1989. }
  1990. /* Get the size from partiton name */
  1991. static void get_partition_size(const char *arg, char *response)
  1992. {
  1993. uint64_t ptn = 0;
  1994. uint64_t size;
  1995. int index = INVALID_PTN;
  1996. index = partition_get_index(arg);
  1997. if (index == INVALID_PTN)
  1998. {
  1999. dprintf(CRITICAL, "Invalid partition index\n");
  2000. return;
  2001. }
  2002. ptn = partition_get_offset(index);
  2003. if(!ptn)
  2004. {
  2005. dprintf(CRITICAL, "Invalid partition name %s\n", arg);
  2006. return;
  2007. }
  2008. size = partition_get_size(index);
  2009. snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
  2010. return;
  2011. }
  2012. /*
  2013. * Publish the partition type & size info
  2014. * fastboot getvar will publish the required information.
  2015. * fastboot getvar partition_size:<partition_name>: partition size in hex
  2016. * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
  2017. */
  2018. static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
  2019. {
  2020. uint8_t i;
  2021. for (i = 0; i < num_parts; i++) {
  2022. get_partition_size(info[i].part_name, info[i].size_response);
  2023. if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
  2024. {
  2025. dprintf(CRITICAL, "partition size name truncated\n");
  2026. return;
  2027. }
  2028. if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
  2029. {
  2030. dprintf(CRITICAL, "partition type name truncated\n");
  2031. return;
  2032. }
  2033. /* publish partition size & type info */
  2034. fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
  2035. fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
  2036. }
  2037. }
  2038. /* register commands and variables for fastboot */
  2039. void aboot_fastboot_register_commands(void)
  2040. {
  2041. if (target_is_emmc_boot())
  2042. {
  2043. fastboot_register("flash:", cmd_flash_mmc);
  2044. fastboot_register("erase:", cmd_erase_mmc);
  2045. }
  2046. else
  2047. {
  2048. fastboot_register("flash:", cmd_flash);
  2049. fastboot_register("erase:", cmd_erase);
  2050. }
  2051. fastboot_register("boot", cmd_boot);
  2052. fastboot_register("continue", cmd_continue);
  2053. fastboot_register("reboot", cmd_reboot);
  2054. fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
  2055. fastboot_register("oem unlock", cmd_oem_unlock);
  2056. fastboot_register("oem device-info", cmd_oem_devinfo);
  2057. fastboot_register("preflash", cmd_preflash);
  2058. fastboot_register("oem enable-charger-screen",
  2059. cmd_oem_enable_charger_screen);
  2060. fastboot_register("oem disable-charger-screen",
  2061. cmd_oem_disable_charger_screen);
  2062. fastboot_register("oem select-display-panel",
  2063. cmd_oem_select_display_panel);
  2064. /* publish variables and their values */
  2065. fastboot_publish("product", TARGET(BOARD));
  2066. fastboot_publish("kernel", "lk");
  2067. fastboot_publish("serialno", sn_buf);
  2068. /*
  2069. * partition info is supported only for emmc partitions
  2070. * Calling this for NAND prints some error messages which
  2071. * is harmless but misleading. Avoid calling this for NAND
  2072. * devices.
  2073. */
  2074. if (target_is_emmc_boot())
  2075. publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
  2076. /* Max download size supported */
  2077. snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
  2078. target_get_max_flash_size());
  2079. fastboot_publish("max-download-size", (const char *) max_download_size);
  2080. /* Is the charger screen check enabled */
  2081. snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
  2082. device.charger_screen_enabled);
  2083. fastboot_publish("charger-screen-enabled",
  2084. (const char *) charger_screen_enabled);
  2085. snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
  2086. device.display_panel);
  2087. fastboot_publish("display-panel",
  2088. (const char *) panel_display_mode);
  2089. }
  2090. void aboot_init(const struct app_descriptor *app)
  2091. {
  2092. unsigned reboot_mode = 0;
  2093. bool boot_into_fastboot = false;
  2094. /* Setup page size information for nv storage */
  2095. if (target_is_emmc_boot())
  2096. {
  2097. page_size = mmc_page_size();
  2098. page_mask = page_size - 1;
  2099. }
  2100. else
  2101. {
  2102. page_size = flash_page_size();
  2103. page_mask = page_size - 1;
  2104. }
  2105. ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
  2106. read_device_info(&device);
  2107. /* Display splash screen if enabled */
  2108. #if DISPLAY_SPLASH_SCREEN
  2109. dprintf(SPEW, "Display Init: Start\n");
  2110. target_display_init(device.display_panel);
  2111. dprintf(SPEW, "Display Init: Done\n");
  2112. #endif
  2113. target_serialno((unsigned char *) sn_buf);
  2114. dprintf(SPEW,"serial number: %s\n",sn_buf);
  2115. memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
  2116. /* Check if we should do something other than booting up */
  2117. if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
  2118. {
  2119. dprintf(ALWAYS,"dload mode key sequence detected\n");
  2120. if (set_download_mode(EMERGENCY_DLOAD))
  2121. {
  2122. dprintf(CRITICAL,"dload mode not supported by target\n");
  2123. }
  2124. else
  2125. {
  2126. reboot_device(DLOAD);
  2127. dprintf(CRITICAL,"Failed to reboot into dload mode\n");
  2128. }
  2129. boot_into_fastboot = true;
  2130. }
  2131. if (!boot_into_fastboot)
  2132. {
  2133. if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
  2134. boot_into_recovery = 1;
  2135. if (!boot_into_recovery &&
  2136. (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
  2137. boot_into_fastboot = true;
  2138. }
  2139. #if NO_KEYPAD_DRIVER
  2140. if (fastboot_trigger())
  2141. boot_into_fastboot = true;
  2142. #endif
  2143. reboot_mode = check_reboot_mode();
  2144. if (reboot_mode == RECOVERY_MODE) {
  2145. boot_into_recovery = 1;
  2146. } else if(reboot_mode == FASTBOOT_MODE) {
  2147. boot_into_fastboot = true;
  2148. }
  2149. if (!boot_into_fastboot)
  2150. {
  2151. if (target_is_emmc_boot())
  2152. {
  2153. if(emmc_recovery_init())
  2154. dprintf(ALWAYS,"error in emmc_recovery_init\n");
  2155. if(target_use_signed_kernel())
  2156. {
  2157. if((device.is_unlocked) || (device.is_tampered))
  2158. {
  2159. #ifdef TZ_TAMPER_FUSE
  2160. set_tamper_fuse_cmd();
  2161. #endif
  2162. #if USE_PCOM_SECBOOT
  2163. set_tamper_flag(device.is_tampered);
  2164. #endif
  2165. }
  2166. }
  2167. boot_linux_from_mmc();
  2168. }
  2169. else
  2170. {
  2171. recovery_init();
  2172. #if USE_PCOM_SECBOOT
  2173. if((device.is_unlocked) || (device.is_tampered))
  2174. set_tamper_flag(device.is_tampered);
  2175. #endif
  2176. boot_linux_from_flash();
  2177. }
  2178. dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
  2179. "to fastboot mode.\n");
  2180. }
  2181. /* We are here means regular boot did not happen. Start fastboot. */
  2182. /* register aboot specific fastboot commands */
  2183. aboot_fastboot_register_commands();
  2184. /* dump partition table for debug info */
  2185. partition_dump();
  2186. /* initialize and start fastboot */
  2187. fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
  2188. }
  2189. uint32_t get_page_size()
  2190. {
  2191. return page_size;
  2192. }
  2193. /*
  2194. * Calculated and save hash (SHA256) for non-signed boot image.
  2195. *
  2196. * Hash the same data that is checked on the signed boot image.
  2197. * Kernel and Ramdisk are already read to memory buffers.
  2198. * Need to read the entire device-tree from mmc
  2199. * since non-signed image only read the DT tags of the relevant platform.
  2200. *
  2201. * @param kernel_addr - kernel bufer
  2202. * @param kernel_actual - kernel size in bytes
  2203. * @param ramdisk_addr - ramdisk buffer
  2204. * @param ramdisk_actual - ramdisk size
  2205. * @param ptn - partition
  2206. * @param dt_offset - device tree offset on mmc partition
  2207. * @param dt_size
  2208. *
  2209. * @return int - 0 on success, negative value on failure.
  2210. */
  2211. int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
  2212. void *ramdisk_addr, unsigned ramdisk_actual,
  2213. unsigned long long ptn,
  2214. unsigned dt_offset, unsigned dt_size)
  2215. {
  2216. SHA256_CTX sha256_ctx;
  2217. char digest[32]={0};
  2218. char *buf = (char *)target_get_scratch_address();
  2219. unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
  2220. unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
  2221. SHA256_Init(&sha256_ctx);
  2222. /* Read Boot Header */
  2223. if (mmc_read(ptn, buf, page_size))
  2224. {
  2225. dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
  2226. return -1;
  2227. }
  2228. /* Read entire Device Tree */
  2229. if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
  2230. {
  2231. dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
  2232. return -1;
  2233. }
  2234. SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
  2235. SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
  2236. SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
  2237. SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
  2238. SHA256_Final(digest, &sha256_ctx);
  2239. save_kernel_hash_cmd(digest);
  2240. dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
  2241. return 0;
  2242. }
  2243. APP_START(aboot)
  2244. .init = aboot_init,
  2245. APP_END