image.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #define DEBUG
  26. #ifndef USE_HOSTCC
  27. #include <common.h>
  28. #include <watchdog.h>
  29. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  30. #include <status_led.h>
  31. #endif
  32. #ifdef CONFIG_HAS_DATAFLASH
  33. #include <dataflash.h>
  34. #endif
  35. #ifdef CONFIG_LOGBUFFER
  36. #include <logbuff.h>
  37. #endif
  38. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
  39. #include <rtc.h>
  40. #endif
  41. #include <image.h>
  42. #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
  43. #include <fdt.h>
  44. #include <libfdt.h>
  45. #include <fdt_support.h>
  46. #endif
  47. #if defined(CONFIG_FIT)
  48. #include <sha1.h>
  49. static int fit_check_ramdisk (const void *fit, int os_noffset,
  50. uint8_t arch, int verify);
  51. #endif
  52. #ifdef CONFIG_CMD_BDI
  53. extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  54. #endif
  55. DECLARE_GLOBAL_DATA_PTR;
  56. static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
  57. int verify);
  58. #else
  59. #include "mkimage.h"
  60. #include <time.h>
  61. #include <image.h>
  62. #endif /* !USE_HOSTCC*/
  63. typedef struct table_entry {
  64. int id; /* as defined in image.h */
  65. char *sname; /* short (input) name */
  66. char *lname; /* long (output) name */
  67. } table_entry_t;
  68. static table_entry_t uimage_arch[] = {
  69. { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
  70. { IH_ARCH_ALPHA, "alpha", "Alpha", },
  71. { IH_ARCH_ARM, "arm", "ARM", },
  72. { IH_ARCH_I386, "x86", "Intel x86", },
  73. { IH_ARCH_IA64, "ia64", "IA64", },
  74. { IH_ARCH_M68K, "m68k", "M68K", },
  75. { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
  76. { IH_ARCH_MIPS, "mips", "MIPS", },
  77. { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
  78. { IH_ARCH_NIOS, "nios", "NIOS", },
  79. { IH_ARCH_NIOS2, "nios2", "NIOS II", },
  80. { IH_ARCH_PPC, "ppc", "PowerPC", },
  81. { IH_ARCH_S390, "s390", "IBM S390", },
  82. { IH_ARCH_SH, "sh", "SuperH", },
  83. { IH_ARCH_SPARC, "sparc", "SPARC", },
  84. { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
  85. { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
  86. { IH_ARCH_AVR32, "avr32", "AVR32", },
  87. { -1, "", "", },
  88. };
  89. static table_entry_t uimage_os[] = {
  90. { IH_OS_INVALID, NULL, "Invalid OS", },
  91. #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
  92. { IH_OS_ARTOS, "artos", "ARTOS", },
  93. #endif
  94. { IH_OS_LINUX, "linux", "Linux", },
  95. #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
  96. { IH_OS_LYNXOS, "lynxos", "LynxOS", },
  97. #endif
  98. { IH_OS_NETBSD, "netbsd", "NetBSD", },
  99. { IH_OS_RTEMS, "rtems", "RTEMS", },
  100. { IH_OS_U_BOOT, "u-boot", "U-Boot", },
  101. #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
  102. { IH_OS_QNX, "qnx", "QNX", },
  103. { IH_OS_VXWORKS, "vxworks", "VxWorks", },
  104. #endif
  105. #ifdef USE_HOSTCC
  106. { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
  107. { IH_OS_DELL, "dell", "Dell", },
  108. { IH_OS_ESIX, "esix", "Esix", },
  109. { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
  110. { IH_OS_IRIX, "irix", "Irix", },
  111. { IH_OS_NCR, "ncr", "NCR", },
  112. { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
  113. { IH_OS_PSOS, "psos", "pSOS", },
  114. { IH_OS_SCO, "sco", "SCO", },
  115. { IH_OS_SOLARIS, "solaris", "Solaris", },
  116. { IH_OS_SVR4, "svr4", "SVR4", },
  117. #endif
  118. { -1, "", "", },
  119. };
  120. static table_entry_t uimage_type[] = {
  121. { IH_TYPE_INVALID, NULL, "Invalid Image", },
  122. { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
  123. { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
  124. { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
  125. { IH_TYPE_MULTI, "multi", "Multi-File Image", },
  126. { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
  127. { IH_TYPE_SCRIPT, "script", "Script", },
  128. { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
  129. { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
  130. { -1, "", "", },
  131. };
  132. static table_entry_t uimage_comp[] = {
  133. { IH_COMP_NONE, "none", "uncompressed", },
  134. { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
  135. { IH_COMP_GZIP, "gzip", "gzip compressed", },
  136. { -1, "", "", },
  137. };
  138. unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
  139. static void genimg_print_size (uint32_t size);
  140. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  141. static void genimg_print_time (time_t timestamp);
  142. #endif
  143. /*****************************************************************************/
  144. /* Legacy format routines */
  145. /*****************************************************************************/
  146. int image_check_hcrc (image_header_t *hdr)
  147. {
  148. ulong hcrc;
  149. ulong len = image_get_header_size ();
  150. image_header_t header;
  151. /* Copy header so we can blank CRC field for re-calculation */
  152. memmove (&header, (char *)hdr, image_get_header_size ());
  153. image_set_hcrc (&header, 0);
  154. hcrc = crc32 (0, (unsigned char *)&header, len);
  155. return (hcrc == image_get_hcrc (hdr));
  156. }
  157. int image_check_dcrc (image_header_t *hdr)
  158. {
  159. ulong data = image_get_data (hdr);
  160. ulong len = image_get_data_size (hdr);
  161. ulong dcrc = crc32 (0, (unsigned char *)data, len);
  162. return (dcrc == image_get_dcrc (hdr));
  163. }
  164. #ifndef USE_HOSTCC
  165. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
  166. {
  167. ulong dcrc = 0;
  168. ulong len = image_get_data_size (hdr);
  169. ulong data = image_get_data (hdr);
  170. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  171. ulong cdata = data;
  172. ulong edata = cdata + len;
  173. while (cdata < edata) {
  174. ulong chunk = edata - cdata;
  175. if (chunk > chunksz)
  176. chunk = chunksz;
  177. dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
  178. cdata += chunk;
  179. WATCHDOG_RESET ();
  180. }
  181. #else
  182. dcrc = crc32 (0, (unsigned char *)data, len);
  183. #endif
  184. return (dcrc == image_get_dcrc (hdr));
  185. }
  186. #endif /* !USE_HOSTCC */
  187. /**
  188. * image_multi_count - get component (sub-image) count
  189. * @hdr: pointer to the header of the multi component image
  190. *
  191. * image_multi_count() returns number of components in a multi
  192. * component image.
  193. *
  194. * Note: no checking of the image type is done, caller must pass
  195. * a valid multi component image.
  196. *
  197. * returns:
  198. * number of components
  199. */
  200. ulong image_multi_count (image_header_t *hdr)
  201. {
  202. ulong i, count = 0;
  203. uint32_t *size;
  204. /* get start of the image payload, which in case of multi
  205. * component images that points to a table of component sizes */
  206. size = (uint32_t *)image_get_data (hdr);
  207. /* count non empty slots */
  208. for (i = 0; size[i]; ++i)
  209. count++;
  210. return count;
  211. }
  212. /**
  213. * image_multi_getimg - get component data address and size
  214. * @hdr: pointer to the header of the multi component image
  215. * @idx: index of the requested component
  216. * @data: pointer to a ulong variable, will hold component data address
  217. * @len: pointer to a ulong variable, will hold component size
  218. *
  219. * image_multi_getimg() returns size and data address for the requested
  220. * component in a multi component image.
  221. *
  222. * Note: no checking of the image type is done, caller must pass
  223. * a valid multi component image.
  224. *
  225. * returns:
  226. * data address and size of the component, if idx is valid
  227. * 0 in data and len, if idx is out of range
  228. */
  229. void image_multi_getimg (image_header_t *hdr, ulong idx,
  230. ulong *data, ulong *len)
  231. {
  232. int i;
  233. uint32_t *size;
  234. ulong offset, tail, count, img_data;
  235. /* get number of component */
  236. count = image_multi_count (hdr);
  237. /* get start of the image payload, which in case of multi
  238. * component images that points to a table of component sizes */
  239. size = (uint32_t *)image_get_data (hdr);
  240. /* get address of the proper component data start, which means
  241. * skipping sizes table (add 1 for last, null entry) */
  242. img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
  243. if (idx < count) {
  244. *len = uimage_to_cpu (size[idx]);
  245. offset = 0;
  246. tail = 0;
  247. /* go over all indices preceding requested component idx */
  248. for (i = 0; i < idx; i++) {
  249. /* add up i-th component size */
  250. offset += uimage_to_cpu (size[i]);
  251. /* add up alignment for i-th component */
  252. tail += (4 - uimage_to_cpu (size[i]) % 4);
  253. }
  254. /* calculate idx-th component data address */
  255. *data = img_data + offset + tail;
  256. } else {
  257. *len = 0;
  258. *data = 0;
  259. }
  260. }
  261. static void image_print_type (image_header_t *hdr)
  262. {
  263. const char *os, *arch, *type, *comp;
  264. os = genimg_get_os_name (image_get_os (hdr));
  265. arch = genimg_get_arch_name (image_get_arch (hdr));
  266. type = genimg_get_type_name (image_get_type (hdr));
  267. comp = genimg_get_comp_name (image_get_comp (hdr));
  268. printf ("%s %s %s (%s)\n", arch, os, type, comp);
  269. }
  270. /**
  271. * __image_print_contents - prints out the contents of the legacy format image
  272. * @hdr: pointer to the legacy format image header
  273. * @p: pointer to prefix string
  274. *
  275. * __image_print_contents() formats a multi line legacy image contents description.
  276. * The routine prints out all header fields followed by the size/offset data
  277. * for MULTI/SCRIPT images.
  278. *
  279. * returns:
  280. * no returned results
  281. */
  282. static void __image_print_contents (image_header_t *hdr, const char *p)
  283. {
  284. printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
  285. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  286. printf ("%sCreated: ", p);
  287. genimg_print_time ((time_t)image_get_time (hdr));
  288. #endif
  289. printf ("%sImage Type: ", p);
  290. image_print_type (hdr);
  291. printf ("%sData Size: ", p);
  292. genimg_print_size (image_get_data_size (hdr));
  293. printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
  294. printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
  295. if (image_check_type (hdr, IH_TYPE_MULTI) ||
  296. image_check_type (hdr, IH_TYPE_SCRIPT)) {
  297. int i;
  298. ulong data, len;
  299. ulong count = image_multi_count (hdr);
  300. printf ("%sContents:\n", p);
  301. for (i = 0; i < count; i++) {
  302. image_multi_getimg (hdr, i, &data, &len);
  303. printf ("%s Image %d: ", p, i);
  304. genimg_print_size (len);
  305. if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
  306. /*
  307. * the user may need to know offsets
  308. * if planning to do something with
  309. * multiple files
  310. */
  311. printf ("%s Offset = 0x%08lx\n", p, data);
  312. }
  313. }
  314. }
  315. }
  316. inline void image_print_contents (image_header_t *hdr)
  317. {
  318. __image_print_contents (hdr, " ");
  319. }
  320. inline void image_print_contents_noindent (image_header_t *hdr)
  321. {
  322. __image_print_contents (hdr, "");
  323. }
  324. #ifndef USE_HOSTCC
  325. /**
  326. * image_get_ramdisk - get and verify ramdisk image
  327. * @rd_addr: ramdisk image start address
  328. * @arch: expected ramdisk architecture
  329. * @verify: checksum verification flag
  330. *
  331. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  332. * header. Routine receives image start address and expected architecture
  333. * flag. Verification done covers data and header integrity and os/type/arch
  334. * fields checking.
  335. *
  336. * If dataflash support is enabled routine checks for dataflash addresses
  337. * and handles required dataflash reads.
  338. *
  339. * returns:
  340. * pointer to a ramdisk image header, if image was found and valid
  341. * otherwise, return NULL
  342. */
  343. static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
  344. int verify)
  345. {
  346. image_header_t *rd_hdr = (image_header_t *)rd_addr;
  347. if (!image_check_magic (rd_hdr)) {
  348. puts ("Bad Magic Number\n");
  349. show_boot_progress (-10);
  350. return NULL;
  351. }
  352. if (!image_check_hcrc (rd_hdr)) {
  353. puts ("Bad Header Checksum\n");
  354. show_boot_progress (-11);
  355. return NULL;
  356. }
  357. show_boot_progress (10);
  358. image_print_contents (rd_hdr);
  359. if (verify) {
  360. puts(" Verifying Checksum ... ");
  361. if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
  362. puts ("Bad Data CRC\n");
  363. show_boot_progress (-12);
  364. return NULL;
  365. }
  366. puts("OK\n");
  367. }
  368. show_boot_progress (11);
  369. if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
  370. !image_check_arch (rd_hdr, arch) ||
  371. !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
  372. printf ("No Linux %s Ramdisk Image\n",
  373. genimg_get_arch_name(arch));
  374. show_boot_progress (-13);
  375. return NULL;
  376. }
  377. return rd_hdr;
  378. }
  379. #endif /* !USE_HOSTCC */
  380. /*****************************************************************************/
  381. /* Shared dual-format routines */
  382. /*****************************************************************************/
  383. #ifndef USE_HOSTCC
  384. int getenv_verify (void)
  385. {
  386. char *s = getenv ("verify");
  387. return (s && (*s == 'n')) ? 0 : 1;
  388. }
  389. int getenv_autostart (void)
  390. {
  391. char *s = getenv ("autostart");
  392. return (s && (*s == 'n')) ? 0 : 1;
  393. }
  394. ulong getenv_bootm_low(void)
  395. {
  396. char *s = getenv ("bootm_low");
  397. if (s) {
  398. ulong tmp = simple_strtoul (s, NULL, 16);
  399. return tmp;
  400. }
  401. #ifdef CFG_SDRAM_BASE
  402. return CFG_SDRAM_BASE;
  403. #else
  404. return 0;
  405. #endif
  406. }
  407. ulong getenv_bootm_size(void)
  408. {
  409. char *s = getenv ("bootm_size");
  410. if (s) {
  411. ulong tmp = simple_strtoul (s, NULL, 16);
  412. return tmp;
  413. }
  414. return gd->bd->bi_memsize;
  415. }
  416. void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
  417. {
  418. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  419. while (len > 0) {
  420. size_t tail = (len > chunksz) ? chunksz : len;
  421. WATCHDOG_RESET ();
  422. memmove (to, from, tail);
  423. to += tail;
  424. from += tail;
  425. len -= tail;
  426. }
  427. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  428. memmove (to, from, len);
  429. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  430. }
  431. #endif /* !USE_HOSTCC */
  432. static void genimg_print_size (uint32_t size)
  433. {
  434. #ifndef USE_HOSTCC
  435. printf ("%d Bytes = ", size);
  436. print_size (size, "\n");
  437. #else
  438. printf ("%d Bytes = %.2f kB = %.2f MB\n",
  439. size, (double)size / 1.024e3,
  440. (double)size / 1.048576e6);
  441. #endif
  442. }
  443. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  444. static void genimg_print_time (time_t timestamp)
  445. {
  446. #ifndef USE_HOSTCC
  447. struct rtc_time tm;
  448. to_tm (timestamp, &tm);
  449. printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  450. tm.tm_year, tm.tm_mon, tm.tm_mday,
  451. tm.tm_hour, tm.tm_min, tm.tm_sec);
  452. #else
  453. printf ("%s", ctime(&timestamp));
  454. #endif
  455. }
  456. #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
  457. /**
  458. * get_table_entry_name - translate entry id to long name
  459. * @table: pointer to a translation table for entries of a specific type
  460. * @msg: message to be returned when translation fails
  461. * @id: entry id to be translated
  462. *
  463. * get_table_entry_name() will go over translation table trying to find
  464. * entry that matches given id. If matching entry is found, its long
  465. * name is returned to the caller.
  466. *
  467. * returns:
  468. * long entry name if translation succeeds
  469. * msg otherwise
  470. */
  471. static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
  472. {
  473. for (; table->id >= 0; ++table) {
  474. if (table->id == id)
  475. return (table->lname);
  476. }
  477. return (msg);
  478. }
  479. const char *genimg_get_os_name (uint8_t os)
  480. {
  481. return (get_table_entry_name (uimage_os, "Unknown OS", os));
  482. }
  483. const char *genimg_get_arch_name (uint8_t arch)
  484. {
  485. return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
  486. }
  487. const char *genimg_get_type_name (uint8_t type)
  488. {
  489. return (get_table_entry_name (uimage_type, "Unknown Image", type));
  490. }
  491. const char *genimg_get_comp_name (uint8_t comp)
  492. {
  493. return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
  494. }
  495. /**
  496. * get_table_entry_id - translate short entry name to id
  497. * @table: pointer to a translation table for entries of a specific type
  498. * @table_name: to be used in case of error
  499. * @name: entry short name to be translated
  500. *
  501. * get_table_entry_id() will go over translation table trying to find
  502. * entry that matches given short name. If matching entry is found,
  503. * its id returned to the caller.
  504. *
  505. * returns:
  506. * entry id if translation succeeds
  507. * -1 otherwise
  508. */
  509. static int get_table_entry_id (table_entry_t *table,
  510. const char *table_name, const char *name)
  511. {
  512. table_entry_t *t;
  513. #ifdef USE_HOSTCC
  514. int first = 1;
  515. for (t = table; t->id >= 0; ++t) {
  516. if (t->sname && strcasecmp(t->sname, name) == 0)
  517. return (t->id);
  518. }
  519. fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
  520. for (t = table; t->id >= 0; ++t) {
  521. if (t->sname == NULL)
  522. continue;
  523. fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
  524. first = 0;
  525. }
  526. fprintf (stderr, "\n");
  527. #else
  528. for (t = table; t->id >= 0; ++t) {
  529. if (t->sname && strcmp(t->sname, name) == 0)
  530. return (t->id);
  531. }
  532. debug ("Invalid %s Type: %s\n", table_name, name);
  533. #endif /* USE_HOSTCC */
  534. return (-1);
  535. }
  536. int genimg_get_os_id (const char *name)
  537. {
  538. return (get_table_entry_id (uimage_os, "OS", name));
  539. }
  540. int genimg_get_arch_id (const char *name)
  541. {
  542. return (get_table_entry_id (uimage_arch, "CPU", name));
  543. }
  544. int genimg_get_type_id (const char *name)
  545. {
  546. return (get_table_entry_id (uimage_type, "Image", name));
  547. }
  548. int genimg_get_comp_id (const char *name)
  549. {
  550. return (get_table_entry_id (uimage_comp, "Compression", name));
  551. }
  552. #ifndef USE_HOSTCC
  553. /**
  554. * genimg_get_format - get image format type
  555. * @img_addr: image start address
  556. *
  557. * genimg_get_format() checks whether provided address points to a valid
  558. * legacy or FIT image.
  559. *
  560. * New uImage format and FDT blob are based on a libfdt. FDT blob
  561. * may be passed directly or embedded in a FIT image. In both situations
  562. * genimg_get_format() must be able to dectect libfdt header.
  563. *
  564. * returns:
  565. * image format type or IMAGE_FORMAT_INVALID if no image is present
  566. */
  567. int genimg_get_format (void *img_addr)
  568. {
  569. ulong format = IMAGE_FORMAT_INVALID;
  570. image_header_t *hdr;
  571. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  572. char *fit_hdr;
  573. #endif
  574. hdr = (image_header_t *)img_addr;
  575. if (image_check_magic(hdr))
  576. format = IMAGE_FORMAT_LEGACY;
  577. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  578. else {
  579. fit_hdr = (char *)img_addr;
  580. if (fdt_check_header (fit_hdr) == 0)
  581. format = IMAGE_FORMAT_FIT;
  582. }
  583. #endif
  584. return format;
  585. }
  586. /**
  587. * genimg_get_image - get image from special storage (if necessary)
  588. * @img_addr: image start address
  589. *
  590. * genimg_get_image() checks if provided image start adddress is located
  591. * in a dataflash storage. If so, image is moved to a system RAM memory.
  592. *
  593. * returns:
  594. * image start address after possible relocation from special storage
  595. */
  596. ulong genimg_get_image (ulong img_addr)
  597. {
  598. ulong ram_addr = img_addr;
  599. #ifdef CONFIG_HAS_DATAFLASH
  600. ulong h_size, d_size;
  601. if (addr_dataflash (img_addr)){
  602. /* ger RAM address */
  603. ram_addr = CFG_LOAD_ADDR;
  604. /* get header size */
  605. h_size = image_get_header_size ();
  606. #if defined(CONFIG_FIT)
  607. if (sizeof(struct fdt_header) > h_size)
  608. h_size = sizeof(struct fdt_header);
  609. #endif
  610. /* read in header */
  611. debug (" Reading image header from dataflash address "
  612. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  613. read_dataflash (img_addr, h_size, (char *)ram_addr);
  614. /* get data size */
  615. switch (genimg_get_format ((void *)ram_addr)) {
  616. case IMAGE_FORMAT_LEGACY:
  617. d_size = image_get_data_size ((image_header_t *)ram_addr);
  618. debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
  619. ram_addr, d_size);
  620. break;
  621. #if defined(CONFIG_FIT)
  622. case IMAGE_FORMAT_FIT:
  623. d_size = fit_get_size ((const void *)ram_addr) - h_size;
  624. debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
  625. ram_addr, d_size);
  626. break;
  627. #endif
  628. default:
  629. printf (" No valid image found at 0x%08lx\n", img_addr);
  630. return ram_addr;
  631. }
  632. /* read in image data */
  633. debug (" Reading image remaining data from dataflash address "
  634. "%08lx to RAM address %08lx\n", img_addr + h_size,
  635. ram_addr + h_size);
  636. read_dataflash (img_addr + h_size, d_size,
  637. (char *)(ram_addr + h_size));
  638. }
  639. #endif /* CONFIG_HAS_DATAFLASH */
  640. return ram_addr;
  641. }
  642. /**
  643. * boot_get_ramdisk - main ramdisk handling routine
  644. * @argc: command argument count
  645. * @argv: command argument list
  646. * @images: pointer to the bootm images structure
  647. * @arch: expected ramdisk architecture
  648. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  649. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  650. *
  651. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  652. * Curently supported are the following ramdisk sources:
  653. * - multicomponent kernel/ramdisk image,
  654. * - commandline provided address of decicated ramdisk image.
  655. *
  656. * returns:
  657. * 0, if ramdisk image was found and valid, or skiped
  658. * rd_start and rd_end are set to ramdisk start/end addresses if
  659. * ramdisk image is found and valid
  660. *
  661. * 1, if ramdisk image is found but corrupted
  662. * rd_start and rd_end are set to 0 if no ramdisk exists
  663. */
  664. int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
  665. uint8_t arch, ulong *rd_start, ulong *rd_end)
  666. {
  667. ulong rd_addr, rd_load;
  668. ulong rd_data, rd_len;
  669. image_header_t *rd_hdr;
  670. #if defined(CONFIG_FIT)
  671. void *fit_hdr;
  672. const char *fit_uname_config = NULL;
  673. const char *fit_uname_ramdisk = NULL;
  674. ulong default_addr;
  675. int rd_noffset;
  676. int conf_noffset;
  677. const void *data;
  678. size_t size;
  679. #endif
  680. *rd_start = 0;
  681. *rd_end = 0;
  682. /*
  683. * Look for a '-' which indicates to ignore the
  684. * ramdisk argument
  685. */
  686. if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
  687. debug ("## Skipping init Ramdisk\n");
  688. rd_len = rd_data = 0;
  689. } else if (argc >= 3) {
  690. #if defined(CONFIG_FIT)
  691. /*
  692. * If the init ramdisk comes from the FIT image and the FIT image
  693. * address is omitted in the command line argument, try to use
  694. * os FIT image address or default load address.
  695. */
  696. if (images->fit_uname_os)
  697. default_addr = (ulong)images->fit_hdr_os;
  698. else
  699. default_addr = load_addr;
  700. if (fit_parse_conf (argv[2], default_addr,
  701. &rd_addr, &fit_uname_config)) {
  702. debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
  703. fit_uname_config, rd_addr);
  704. } else if (fit_parse_subimage (argv[2], default_addr,
  705. &rd_addr, &fit_uname_ramdisk)) {
  706. debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
  707. fit_uname_ramdisk, rd_addr);
  708. } else
  709. #endif
  710. {
  711. rd_addr = simple_strtoul(argv[2], NULL, 16);
  712. debug ("* ramdisk: cmdline image address = 0x%08lx\n",
  713. rd_addr);
  714. }
  715. /* copy from dataflash if needed */
  716. rd_addr = genimg_get_image (rd_addr);
  717. /*
  718. * Check if there is an initrd image at the
  719. * address provided in the second bootm argument
  720. * check image type, for FIT images get FIT node.
  721. */
  722. switch (genimg_get_format ((void *)rd_addr)) {
  723. case IMAGE_FORMAT_LEGACY:
  724. printf ("## Loading init Ramdisk from Legacy "
  725. "Image at %08lx ...\n", rd_addr);
  726. show_boot_progress (9);
  727. rd_hdr = image_get_ramdisk (rd_addr, arch,
  728. images->verify);
  729. if (rd_hdr == NULL)
  730. return 1;
  731. rd_data = image_get_data (rd_hdr);
  732. rd_len = image_get_data_size (rd_hdr);
  733. rd_load = image_get_load (rd_hdr);
  734. break;
  735. #if defined(CONFIG_FIT)
  736. case IMAGE_FORMAT_FIT:
  737. fit_hdr = (void *)rd_addr;
  738. printf ("## Loading init Ramdisk from FIT "
  739. "Image at %08lx ...\n", rd_addr);
  740. show_boot_progress (120);
  741. if (!fit_check_format (fit_hdr)) {
  742. puts ("Bad FIT ramdisk image format!\n");
  743. show_boot_progress (-120);
  744. return 0;
  745. }
  746. show_boot_progress (121);
  747. if (!fit_uname_ramdisk) {
  748. /*
  749. * no ramdisk image node unit name, try to get config
  750. * node first. If config unit node name is NULL
  751. * fit_conf_get_node() will try to find default config node
  752. */
  753. show_boot_progress (122);
  754. conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
  755. if (conf_noffset < 0) {
  756. show_boot_progress (-122);
  757. return 0;
  758. }
  759. rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, conf_noffset);
  760. fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
  761. } else {
  762. /* get ramdisk component image node offset */
  763. show_boot_progress (123);
  764. rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
  765. }
  766. if (rd_noffset < 0) {
  767. show_boot_progress (-124);
  768. return 0;
  769. }
  770. printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
  771. show_boot_progress (125);
  772. if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
  773. return 0;
  774. /* get ramdisk image data address and length */
  775. if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
  776. puts ("Could not find ramdisk subimage data!\n");
  777. show_boot_progress (-127);
  778. return 0;
  779. }
  780. show_boot_progress (128);
  781. rd_data = (ulong)data;
  782. rd_len = size;
  783. if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
  784. puts ("Can't get ramdisk subimage load address!\n");
  785. show_boot_progress (-129);
  786. return 0;
  787. }
  788. show_boot_progress (129);
  789. images->fit_hdr_rd = fit_hdr;
  790. images->fit_uname_rd = fit_uname_ramdisk;
  791. images->fit_noffset_rd = rd_noffset;
  792. break;
  793. #endif
  794. default:
  795. puts ("Wrong Ramdisk Image Format\n");
  796. rd_data = rd_len = rd_load = 0;
  797. }
  798. #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
  799. /*
  800. * We need to copy the ramdisk to SRAM to let Linux boot
  801. */
  802. if (rd_data) {
  803. memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
  804. rd_data = rd_load;
  805. }
  806. #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
  807. } else if (images->legacy_hdr_valid &&
  808. image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
  809. /*
  810. * Now check if we have a legacy mult-component image,
  811. * get second entry data start address and len.
  812. */
  813. show_boot_progress (13);
  814. printf ("## Loading init Ramdisk from multi component "
  815. "Legacy Image at %08lx ...\n",
  816. (ulong)images->legacy_hdr_os);
  817. image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
  818. } else {
  819. /*
  820. * no initrd image
  821. */
  822. show_boot_progress (14);
  823. rd_len = rd_data = 0;
  824. }
  825. if (!rd_data) {
  826. debug ("## No init Ramdisk\n");
  827. } else {
  828. *rd_start = rd_data;
  829. *rd_end = rd_data + rd_len;
  830. }
  831. debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  832. *rd_start, *rd_end);
  833. return 0;
  834. }
  835. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  836. /**
  837. * boot_ramdisk_high - relocate init ramdisk
  838. * @lmb: pointer to lmb handle, will be used for memory mgmt
  839. * @rd_data: ramdisk data start address
  840. * @rd_len: ramdisk data length
  841. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  842. * start address (after possible relocation)
  843. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  844. * end address (after possible relocation)
  845. *
  846. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
  847. * variable and if requested ramdisk data is moved to a specified location.
  848. *
  849. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  850. * start/end addresses if ramdisk image start and len were provided,
  851. * otherwise set initrd_start and initrd_end set to zeros.
  852. *
  853. * returns:
  854. * 0 - success
  855. * -1 - failure
  856. */
  857. int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
  858. ulong *initrd_start, ulong *initrd_end)
  859. {
  860. char *s;
  861. ulong initrd_high;
  862. int initrd_copy_to_ram = 1;
  863. if ((s = getenv ("initrd_high")) != NULL) {
  864. /* a value of "no" or a similar string will act like 0,
  865. * turning the "load high" feature off. This is intentional.
  866. */
  867. initrd_high = simple_strtoul (s, NULL, 16);
  868. if (initrd_high == ~0)
  869. initrd_copy_to_ram = 0;
  870. } else {
  871. /* not set, no restrictions to load high */
  872. initrd_high = ~0;
  873. }
  874. debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  875. initrd_high, initrd_copy_to_ram);
  876. if (rd_data) {
  877. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  878. debug (" in-place initrd\n");
  879. *initrd_start = rd_data;
  880. *initrd_end = rd_data + rd_len;
  881. lmb_reserve(lmb, rd_data, rd_len);
  882. } else {
  883. if (initrd_high)
  884. *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
  885. else
  886. *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
  887. if (*initrd_start == 0) {
  888. puts ("ramdisk - allocation error\n");
  889. goto error;
  890. }
  891. show_boot_progress (12);
  892. *initrd_end = *initrd_start + rd_len;
  893. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  894. *initrd_start, *initrd_end);
  895. memmove_wd ((void *)*initrd_start,
  896. (void *)rd_data, rd_len, CHUNKSZ);
  897. puts ("OK\n");
  898. }
  899. } else {
  900. *initrd_start = 0;
  901. *initrd_end = 0;
  902. }
  903. debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  904. *initrd_start, *initrd_end);
  905. return 0;
  906. error:
  907. return -1;
  908. }
  909. /**
  910. * boot_get_cmdline - allocate and initialize kernel cmdline
  911. * @lmb: pointer to lmb handle, will be used for memory mgmt
  912. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  913. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  914. * @bootmap_base: ulong variable, holds offset in physical memory to
  915. * base of bootmap
  916. *
  917. * boot_get_cmdline() allocates space for kernel command line below
  918. * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
  919. * variable is present its contents is copied to allocated kernel
  920. * command line.
  921. *
  922. * returns:
  923. * 0 - success
  924. * -1 - failure
  925. */
  926. int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
  927. ulong bootmap_base)
  928. {
  929. char *cmdline;
  930. char *s;
  931. cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
  932. CFG_BOOTMAPSZ + bootmap_base);
  933. if (cmdline == NULL)
  934. return -1;
  935. if ((s = getenv("bootargs")) == NULL)
  936. s = "";
  937. strcpy(cmdline, s);
  938. *cmd_start = (ulong) & cmdline[0];
  939. *cmd_end = *cmd_start + strlen(cmdline);
  940. debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  941. return 0;
  942. }
  943. /**
  944. * boot_get_kbd - allocate and initialize kernel copy of board info
  945. * @lmb: pointer to lmb handle, will be used for memory mgmt
  946. * @kbd: double pointer to board info data
  947. * @bootmap_base: ulong variable, holds offset in physical memory to
  948. * base of bootmap
  949. *
  950. * boot_get_kbd() allocates space for kernel copy of board info data below
  951. * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
  952. * the current u-boot board info data.
  953. *
  954. * returns:
  955. * 0 - success
  956. * -1 - failure
  957. */
  958. int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
  959. {
  960. *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  961. CFG_BOOTMAPSZ + bootmap_base);
  962. if (*kbd == NULL)
  963. return -1;
  964. **kbd = *(gd->bd);
  965. debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  966. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  967. do_bdinfo(NULL, 0, 0, NULL);
  968. #endif
  969. return 0;
  970. }
  971. #endif /* CONFIG_PPC || CONFIG_M68K */
  972. #endif /* !USE_HOSTCC */
  973. #if defined(CONFIG_FIT)
  974. /*****************************************************************************/
  975. /* New uImage format routines */
  976. /*****************************************************************************/
  977. #ifndef USE_HOSTCC
  978. static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
  979. ulong *addr, const char **name)
  980. {
  981. const char *sep;
  982. *addr = addr_curr;
  983. *name = NULL;
  984. sep = strchr (spec, sepc);
  985. if (sep) {
  986. if (sep - spec > 0)
  987. *addr = simple_strtoul (spec, NULL, 16);
  988. *name = sep + 1;
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. /**
  994. * fit_parse_conf - parse FIT configuration spec
  995. * @spec: input string, containing configuration spec
  996. * @add_curr: current image address (to be used as a possible default)
  997. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  998. * configuration
  999. * @conf_name double pointer to a char, will hold pointer to a configuration
  1000. * unit name
  1001. *
  1002. * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
  1003. * where <addr> is a FIT image address that contains configuration
  1004. * with a <conf> unit name.
  1005. *
  1006. * Address part is optional, and if omitted default add_curr will
  1007. * be used instead.
  1008. *
  1009. * returns:
  1010. * 1 if spec is a valid configuration string,
  1011. * addr and conf_name are set accordingly
  1012. * 0 otherwise
  1013. */
  1014. inline int fit_parse_conf (const char *spec, ulong addr_curr,
  1015. ulong *addr, const char **conf_name)
  1016. {
  1017. return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
  1018. }
  1019. /**
  1020. * fit_parse_subimage - parse FIT subimage spec
  1021. * @spec: input string, containing subimage spec
  1022. * @add_curr: current image address (to be used as a possible default)
  1023. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  1024. * subimage
  1025. * @image_name: double pointer to a char, will hold pointer to a subimage name
  1026. *
  1027. * fit_parse_subimage() expects subimage spec in the for of
  1028. * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
  1029. * subimage with a <subimg> unit name.
  1030. *
  1031. * Address part is optional, and if omitted default add_curr will
  1032. * be used instead.
  1033. *
  1034. * returns:
  1035. * 1 if spec is a valid subimage string,
  1036. * addr and image_name are set accordingly
  1037. * 0 otherwise
  1038. */
  1039. inline int fit_parse_subimage (const char *spec, ulong addr_curr,
  1040. ulong *addr, const char **image_name)
  1041. {
  1042. return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
  1043. }
  1044. #endif /* !USE_HOSTCC */
  1045. static void fit_get_debug (const void *fit, int noffset,
  1046. char *prop_name, int err)
  1047. {
  1048. debug ("Can't get '%s' property from FIT 0x%08lx, "
  1049. "node: offset %d, name %s (%s)\n",
  1050. prop_name, (ulong)fit, noffset,
  1051. fit_get_name (fit, noffset, NULL),
  1052. fdt_strerror (err));
  1053. }
  1054. /**
  1055. * __fit_print_contents - prints out the contents of the FIT format image
  1056. * @fit: pointer to the FIT format image header
  1057. * @p: pointer to prefix string
  1058. *
  1059. * __fit_print_contents() formats a multi line FIT image contents description.
  1060. * The routine prints out FIT image properties (root node level) follwed by
  1061. * the details of each component image.
  1062. *
  1063. * returns:
  1064. * no returned results
  1065. */
  1066. static void __fit_print_contents (const void *fit, const char *p)
  1067. {
  1068. char *desc;
  1069. char *uname;
  1070. int images_noffset;
  1071. int confs_noffset;
  1072. int noffset;
  1073. int ndepth;
  1074. int count = 0;
  1075. int ret;
  1076. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  1077. time_t timestamp;
  1078. #endif
  1079. /* Root node properties */
  1080. ret = fit_get_desc (fit, 0, &desc);
  1081. printf ("%sFIT description: ", p);
  1082. if (ret)
  1083. printf ("unavailable\n");
  1084. else
  1085. printf ("%s\n", desc);
  1086. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  1087. ret = fit_get_timestamp (fit, 0, &timestamp);
  1088. printf ("%sCreated: ", p);
  1089. if (ret)
  1090. printf ("unavailable\n");
  1091. else
  1092. genimg_print_time (timestamp);
  1093. #endif
  1094. /* Find images parent node offset */
  1095. images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
  1096. if (images_noffset < 0) {
  1097. printf ("Can't find images parent node '%s' (%s)\n",
  1098. FIT_IMAGES_PATH, fdt_strerror (images_noffset));
  1099. return;
  1100. }
  1101. /* Process its subnodes, print out component images details */
  1102. for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
  1103. (noffset >= 0) && (ndepth > 0);
  1104. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1105. if (ndepth == 1) {
  1106. /*
  1107. * Direct child node of the images parent node,
  1108. * i.e. component image node.
  1109. */
  1110. printf ("%s Image %u (%s)\n", p, count++,
  1111. fit_get_name(fit, noffset, NULL));
  1112. fit_image_print (fit, noffset, p);
  1113. }
  1114. }
  1115. /* Find configurations parent node offset */
  1116. confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
  1117. if (confs_noffset < 0) {
  1118. debug ("Can't get configurations parent node '%s' (%s)\n",
  1119. FIT_CONFS_PATH, fdt_strerror (confs_noffset));
  1120. return;
  1121. }
  1122. /* get default configuration unit name from default property */
  1123. uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
  1124. if (uname)
  1125. printf ("%s Default Configuration: '%s'\n", p, uname);
  1126. /* Process its subnodes, print out configurations details */
  1127. for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
  1128. (noffset >= 0) && (ndepth > 0);
  1129. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1130. if (ndepth == 1) {
  1131. /*
  1132. * Direct child node of the configurations parent node,
  1133. * i.e. configuration node.
  1134. */
  1135. printf ("%s Configuration %u (%s)\n", p, count++,
  1136. fit_get_name(fit, noffset, NULL));
  1137. fit_conf_print (fit, noffset, p);
  1138. }
  1139. }
  1140. }
  1141. inline void fit_print_contents (const void *fit)
  1142. {
  1143. __fit_print_contents (fit, " ");
  1144. }
  1145. inline void fit_print_contents_noindent (const void *fit)
  1146. {
  1147. __fit_print_contents (fit, "");
  1148. }
  1149. /**
  1150. * fit_image_print - prints out the FIT component image details
  1151. * @fit: pointer to the FIT format image header
  1152. * @image_noffset: offset of the component image node
  1153. * @p: pointer to prefix string
  1154. *
  1155. * fit_image_print() lists all mandatory properies for the processed component
  1156. * image. If present, hash nodes are printed out as well.
  1157. *
  1158. * returns:
  1159. * no returned results
  1160. */
  1161. void fit_image_print (const void *fit, int image_noffset, const char *p)
  1162. {
  1163. char *desc;
  1164. uint8_t type, arch, os, comp;
  1165. size_t size;
  1166. ulong load, entry;
  1167. const void *data;
  1168. int noffset;
  1169. int ndepth;
  1170. int ret;
  1171. /* Mandatory properties */
  1172. ret = fit_get_desc (fit, image_noffset, &desc);
  1173. printf ("%s Description: ", p);
  1174. if (ret)
  1175. printf ("unavailable\n");
  1176. else
  1177. printf ("%s\n", desc);
  1178. fit_image_get_type (fit, image_noffset, &type);
  1179. printf ("%s Type: %s\n", p, genimg_get_type_name (type));
  1180. fit_image_get_comp (fit, image_noffset, &comp);
  1181. printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
  1182. ret = fit_image_get_data (fit, image_noffset, &data, &size);
  1183. #ifndef USE_HOSTCC
  1184. printf ("%s Data Start: ", p);
  1185. if (ret)
  1186. printf ("unavailable\n");
  1187. else
  1188. printf ("0x%08lx\n", (ulong)data);
  1189. #endif
  1190. printf ("%s Data Size: ", p);
  1191. if (ret)
  1192. printf ("unavailable\n");
  1193. else
  1194. genimg_print_size (size);
  1195. /* Remaining, type dependent properties */
  1196. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  1197. (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
  1198. (type == IH_TYPE_FLATDT)) {
  1199. fit_image_get_arch (fit, image_noffset, &arch);
  1200. printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
  1201. }
  1202. if (type == IH_TYPE_KERNEL) {
  1203. fit_image_get_os (fit, image_noffset, &os);
  1204. printf ("%s OS: %s\n", p, genimg_get_os_name (os));
  1205. }
  1206. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
  1207. ret = fit_image_get_load (fit, image_noffset, &load);
  1208. printf ("%s Load Address: ", p);
  1209. if (ret)
  1210. printf ("unavailable\n");
  1211. else
  1212. printf ("0x%08lx\n", load);
  1213. fit_image_get_entry (fit, image_noffset, &entry);
  1214. printf ("%s Entry Point: ", p);
  1215. if (ret)
  1216. printf ("unavailable\n");
  1217. else
  1218. printf ("0x%08lx\n", entry);
  1219. }
  1220. /* Process all hash subnodes of the component image node */
  1221. for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
  1222. (noffset >= 0) && (ndepth > 0);
  1223. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1224. if (ndepth == 1) {
  1225. /* Direct child node of the component image node */
  1226. fit_image_print_hash (fit, noffset, p);
  1227. }
  1228. }
  1229. }
  1230. /**
  1231. * fit_image_print_hash - prints out the hash node details
  1232. * @fit: pointer to the FIT format image header
  1233. * @noffset: offset of the hash node
  1234. * @p: pointer to prefix string
  1235. *
  1236. * fit_image_print_hash() lists properies for the processed hash node
  1237. *
  1238. * returns:
  1239. * no returned results
  1240. */
  1241. void fit_image_print_hash (const void *fit, int noffset, const char *p)
  1242. {
  1243. char *algo;
  1244. uint8_t *value;
  1245. int value_len;
  1246. int i, ret;
  1247. /*
  1248. * Check subnode name, must be equal to "hash".
  1249. * Multiple hash nodes require unique unit node
  1250. * names, e.g. hash@1, hash@2, etc.
  1251. */
  1252. if (strncmp (fit_get_name(fit, noffset, NULL),
  1253. FIT_HASH_NODENAME,
  1254. strlen(FIT_HASH_NODENAME)) != 0)
  1255. return;
  1256. debug ("%s Hash node: '%s'\n", p,
  1257. fit_get_name (fit, noffset, NULL));
  1258. printf ("%s Hash algo: ", p);
  1259. if (fit_image_hash_get_algo (fit, noffset, &algo)) {
  1260. printf ("invalid/unsupported\n");
  1261. return;
  1262. }
  1263. printf ("%s\n", algo);
  1264. ret = fit_image_hash_get_value (fit, noffset, &value,
  1265. &value_len);
  1266. printf ("%s Hash value: ", p);
  1267. if (ret) {
  1268. printf ("unavailable\n");
  1269. } else {
  1270. for (i = 0; i < value_len; i++)
  1271. printf ("%02x", value[i]);
  1272. printf ("\n");
  1273. }
  1274. debug ("%s Hash len: %d\n", p, value_len);
  1275. }
  1276. /**
  1277. * fit_get_desc - get node description property
  1278. * @fit: pointer to the FIT format image header
  1279. * @noffset: node offset
  1280. * @desc: double pointer to the char, will hold pointer to the descrption
  1281. *
  1282. * fit_get_desc() reads description property from a given node, if
  1283. * description is found pointer to it is returened in third call argument.
  1284. *
  1285. * returns:
  1286. * 0, on success
  1287. * -1, on failure
  1288. */
  1289. int fit_get_desc (const void *fit, int noffset, char **desc)
  1290. {
  1291. int len;
  1292. *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
  1293. if (*desc == NULL) {
  1294. fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
  1295. return -1;
  1296. }
  1297. return 0;
  1298. }
  1299. /**
  1300. * fit_get_timestamp - get node timestamp property
  1301. * @fit: pointer to the FIT format image header
  1302. * @noffset: node offset
  1303. * @timestamp: pointer to the time_t, will hold read timestamp
  1304. *
  1305. * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
  1306. * is found and has a correct size its value is retured in third call
  1307. * argument.
  1308. *
  1309. * returns:
  1310. * 0, on success
  1311. * -1, on property read failure
  1312. * -2, on wrong timestamp size
  1313. */
  1314. int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
  1315. {
  1316. int len;
  1317. const void *data;
  1318. data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
  1319. if (data == NULL) {
  1320. fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
  1321. return -1;
  1322. }
  1323. if (len != sizeof (uint32_t)) {
  1324. debug ("FIT timestamp with incorrect size of (%u)\n", len);
  1325. return -2;
  1326. }
  1327. *timestamp = uimage_to_cpu (*((uint32_t *)data));
  1328. return 0;
  1329. }
  1330. /**
  1331. * fit_image_get_node - get node offset for component image of a given unit name
  1332. * @fit: pointer to the FIT format image header
  1333. * @image_uname: component image node unit name
  1334. *
  1335. * fit_image_get_node() finds a component image (withing the '/images'
  1336. * node) of a provided unit name. If image is found its node offset is
  1337. * returned to the caller.
  1338. *
  1339. * returns:
  1340. * image node offset when found (>=0)
  1341. * negative number on failure (FDT_ERR_* code)
  1342. */
  1343. int fit_image_get_node (const void *fit, const char *image_uname)
  1344. {
  1345. int noffset, images_noffset;
  1346. images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
  1347. if (images_noffset < 0) {
  1348. debug ("Can't find images parent node '%s' (%s)\n",
  1349. FIT_IMAGES_PATH, fdt_strerror (images_noffset));
  1350. return images_noffset;
  1351. }
  1352. noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
  1353. if (noffset < 0) {
  1354. debug ("Can't get node offset for image unit name: '%s' (%s)\n",
  1355. image_uname, fdt_strerror (noffset));
  1356. }
  1357. return noffset;
  1358. }
  1359. /**
  1360. * fit_image_get_os - get os id for a given component image node
  1361. * @fit: pointer to the FIT format image header
  1362. * @noffset: component image node offset
  1363. * @os: pointer to the uint8_t, will hold os numeric id
  1364. *
  1365. * fit_image_get_os() finds os property in a given component image node.
  1366. * If the property is found, its (string) value is translated to the numeric
  1367. * id which is returned to the caller.
  1368. *
  1369. * returns:
  1370. * 0, on success
  1371. * -1, on failure
  1372. */
  1373. int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
  1374. {
  1375. int len;
  1376. const void *data;
  1377. /* Get OS name from property data */
  1378. data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
  1379. if (data == NULL) {
  1380. fit_get_debug (fit, noffset, FIT_OS_PROP, len);
  1381. *os = -1;
  1382. return -1;
  1383. }
  1384. /* Translate OS name to id */
  1385. *os = genimg_get_os_id (data);
  1386. return 0;
  1387. }
  1388. /**
  1389. * fit_image_get_arch - get arch id for a given component image node
  1390. * @fit: pointer to the FIT format image header
  1391. * @noffset: component image node offset
  1392. * @arch: pointer to the uint8_t, will hold arch numeric id
  1393. *
  1394. * fit_image_get_arch() finds arch property in a given component image node.
  1395. * If the property is found, its (string) value is translated to the numeric
  1396. * id which is returned to the caller.
  1397. *
  1398. * returns:
  1399. * 0, on success
  1400. * -1, on failure
  1401. */
  1402. int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
  1403. {
  1404. int len;
  1405. const void *data;
  1406. /* Get architecture name from property data */
  1407. data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
  1408. if (data == NULL) {
  1409. fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
  1410. *arch = -1;
  1411. return -1;
  1412. }
  1413. /* Translate architecture name to id */
  1414. *arch = genimg_get_arch_id (data);
  1415. return 0;
  1416. }
  1417. /**
  1418. * fit_image_get_type - get type id for a given component image node
  1419. * @fit: pointer to the FIT format image header
  1420. * @noffset: component image node offset
  1421. * @type: pointer to the uint8_t, will hold type numeric id
  1422. *
  1423. * fit_image_get_type() finds type property in a given component image node.
  1424. * If the property is found, its (string) value is translated to the numeric
  1425. * id which is returned to the caller.
  1426. *
  1427. * returns:
  1428. * 0, on success
  1429. * -1, on failure
  1430. */
  1431. int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
  1432. {
  1433. int len;
  1434. const void *data;
  1435. /* Get image type name from property data */
  1436. data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
  1437. if (data == NULL) {
  1438. fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
  1439. *type = -1;
  1440. return -1;
  1441. }
  1442. /* Translate image type name to id */
  1443. *type = genimg_get_type_id (data);
  1444. return 0;
  1445. }
  1446. /**
  1447. * fit_image_get_comp - get comp id for a given component image node
  1448. * @fit: pointer to the FIT format image header
  1449. * @noffset: component image node offset
  1450. * @comp: pointer to the uint8_t, will hold comp numeric id
  1451. *
  1452. * fit_image_get_comp() finds comp property in a given component image node.
  1453. * If the property is found, its (string) value is translated to the numeric
  1454. * id which is returned to the caller.
  1455. *
  1456. * returns:
  1457. * 0, on success
  1458. * -1, on failure
  1459. */
  1460. int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
  1461. {
  1462. int len;
  1463. const void *data;
  1464. /* Get compression name from property data */
  1465. data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
  1466. if (data == NULL) {
  1467. fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
  1468. *comp = -1;
  1469. return -1;
  1470. }
  1471. /* Translate compression name to id */
  1472. *comp = genimg_get_comp_id (data);
  1473. return 0;
  1474. }
  1475. /**
  1476. * fit_image_get_load - get load address property for a given component image node
  1477. * @fit: pointer to the FIT format image header
  1478. * @noffset: component image node offset
  1479. * @load: pointer to the uint32_t, will hold load address
  1480. *
  1481. * fit_image_get_load() finds load address property in a given component image node.
  1482. * If the property is found, its value is returned to the caller.
  1483. *
  1484. * returns:
  1485. * 0, on success
  1486. * -1, on failure
  1487. */
  1488. int fit_image_get_load (const void *fit, int noffset, ulong *load)
  1489. {
  1490. int len;
  1491. const uint32_t *data;
  1492. data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
  1493. if (data == NULL) {
  1494. fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
  1495. return -1;
  1496. }
  1497. *load = uimage_to_cpu (*data);
  1498. return 0;
  1499. }
  1500. /**
  1501. * fit_image_get_entry - get entry point address property for a given component image node
  1502. * @fit: pointer to the FIT format image header
  1503. * @noffset: component image node offset
  1504. * @entry: pointer to the uint32_t, will hold entry point address
  1505. *
  1506. * fit_image_get_entry() finds entry point address property in a given component image node.
  1507. * If the property is found, its value is returned to the caller.
  1508. *
  1509. * returns:
  1510. * 0, on success
  1511. * -1, on failure
  1512. */
  1513. int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
  1514. {
  1515. int len;
  1516. const uint32_t *data;
  1517. data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
  1518. if (data == NULL) {
  1519. fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
  1520. return -1;
  1521. }
  1522. *entry = uimage_to_cpu (*data);
  1523. return 0;
  1524. }
  1525. /**
  1526. * fit_image_get_data - get data property and its size for a given component image node
  1527. * @fit: pointer to the FIT format image header
  1528. * @noffset: component image node offset
  1529. * @data: double pointer to void, will hold data property's data address
  1530. * @size: pointer to size_t, will hold data property's data size
  1531. *
  1532. * fit_image_get_data() finds data property in a given component image node.
  1533. * If the property is found its data start address and size are returned to
  1534. * the caller.
  1535. *
  1536. * returns:
  1537. * 0, on success
  1538. * -1, on failure
  1539. */
  1540. int fit_image_get_data (const void *fit, int noffset,
  1541. const void **data, size_t *size)
  1542. {
  1543. int len;
  1544. *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
  1545. if (*data == NULL) {
  1546. fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
  1547. *size = 0;
  1548. return -1;
  1549. }
  1550. *size = len;
  1551. return 0;
  1552. }
  1553. /**
  1554. * fit_image_hash_get_algo - get hash algorithm name
  1555. * @fit: pointer to the FIT format image header
  1556. * @noffset: hash node offset
  1557. * @algo: double pointer to char, will hold pointer to the algorithm name
  1558. *
  1559. * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
  1560. * If the property is found its data start address is returned to the caller.
  1561. *
  1562. * returns:
  1563. * 0, on success
  1564. * -1, on failure
  1565. */
  1566. int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
  1567. {
  1568. int len;
  1569. *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
  1570. if (*algo == NULL) {
  1571. fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
  1572. return -1;
  1573. }
  1574. return 0;
  1575. }
  1576. /**
  1577. * fit_image_hash_get_value - get hash value and length
  1578. * @fit: pointer to the FIT format image header
  1579. * @noffset: hash node offset
  1580. * @value: double pointer to uint8_t, will hold address of a hash value data
  1581. * @value_len: pointer to an int, will hold hash data length
  1582. *
  1583. * fit_image_hash_get_value() finds hash value property in a given hash node.
  1584. * If the property is found its data start address and size are returned to
  1585. * the caller.
  1586. *
  1587. * returns:
  1588. * 0, on success
  1589. * -1, on failure
  1590. */
  1591. int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
  1592. int *value_len)
  1593. {
  1594. int len;
  1595. *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
  1596. if (*value == NULL) {
  1597. fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
  1598. *value_len = 0;
  1599. return -1;
  1600. }
  1601. *value_len = len;
  1602. return 0;
  1603. }
  1604. /**
  1605. * fit_set_timestamp - set node timestamp property
  1606. * @fit: pointer to the FIT format image header
  1607. * @noffset: node offset
  1608. * @timestamp: timestamp value to be set
  1609. *
  1610. * fit_set_timestamp() attempts to set timestamp property in the requested
  1611. * node and returns operation status to the caller.
  1612. *
  1613. * returns:
  1614. * 0, on success
  1615. * -1, on property read failure
  1616. */
  1617. int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
  1618. {
  1619. uint32_t t;
  1620. int ret;
  1621. t = cpu_to_uimage (timestamp);
  1622. ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
  1623. sizeof (uint32_t));
  1624. if (ret) {
  1625. printf ("Can't set '%s' property for '%s' node (%s)\n",
  1626. FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
  1627. fdt_strerror (ret));
  1628. return -1;
  1629. }
  1630. return 0;
  1631. }
  1632. /**
  1633. * calculate_hash - calculate and return hash for provided input data
  1634. * @data: pointer to the input data
  1635. * @data_len: data length
  1636. * @algo: requested hash algorithm
  1637. * @value: pointer to the char, will hold hash value data (caller must
  1638. * allocate enough free space)
  1639. * value_len: length of the calculated hash
  1640. *
  1641. * calculate_hash() computes input data hash according to the requested algorithm.
  1642. * Resulting hash value is placed in caller provided 'value' buffer, length
  1643. * of the calculated hash is returned via value_len pointer argument.
  1644. *
  1645. * returns:
  1646. * 0, on success
  1647. * -1, when algo is unsupported
  1648. */
  1649. static int calculate_hash (const void *data, int data_len, const char *algo,
  1650. uint8_t *value, int *value_len)
  1651. {
  1652. if (strcmp (algo, "crc32") == 0 ) {
  1653. *((uint32_t *)value) = crc32 (0, data, data_len);
  1654. *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
  1655. *value_len = 4;
  1656. } else if (strcmp (algo, "sha1") == 0 ) {
  1657. sha1_csum ((unsigned char *) data, data_len,
  1658. (unsigned char *) value);
  1659. *value_len = 20;
  1660. } else if (strcmp (algo, "md5") == 0 ) {
  1661. printf ("MD5 not supported\n");
  1662. *value_len = 0;
  1663. } else {
  1664. debug ("Unsupported hash alogrithm\n");
  1665. return -1;
  1666. }
  1667. return 0;
  1668. }
  1669. #ifdef USE_HOSTCC
  1670. /**
  1671. * fit_set_hashes - process FIT component image nodes and calculate hashes
  1672. * @fit: pointer to the FIT format image header
  1673. *
  1674. * fit_set_hashes() adds hash values for all component images in the FIT blob.
  1675. * Hashes are calculated for all component images which have hash subnodes
  1676. * with algorithm property set to one of the supported hash algorithms.
  1677. *
  1678. * returns
  1679. * 0, on success
  1680. * libfdt error code, on failure
  1681. */
  1682. int fit_set_hashes (void *fit)
  1683. {
  1684. int images_noffset;
  1685. int noffset;
  1686. int ndepth;
  1687. int ret;
  1688. /* Find images parent node offset */
  1689. images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
  1690. if (images_noffset < 0) {
  1691. printf ("Can't find images parent node '%s' (%s)\n",
  1692. FIT_IMAGES_PATH, fdt_strerror (images_noffset));
  1693. return images_noffset;
  1694. }
  1695. /* Process its subnodes, print out component images details */
  1696. for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
  1697. (noffset >= 0) && (ndepth > 0);
  1698. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1699. if (ndepth == 1) {
  1700. /*
  1701. * Direct child node of the images parent node,
  1702. * i.e. component image node.
  1703. */
  1704. ret = fit_image_set_hashes (fit, noffset);
  1705. if (ret)
  1706. return ret;
  1707. }
  1708. }
  1709. return 0;
  1710. }
  1711. /**
  1712. * fit_image_set_hashes - calculate/set hashes for given component image node
  1713. * @fit: pointer to the FIT format image header
  1714. * @image_noffset: requested component image node
  1715. *
  1716. * fit_image_set_hashes() adds hash values for an component image node. All
  1717. * existing hash subnodes are checked, if algorithm property is set to one of
  1718. * the supported hash algorithms, hash value is computed and corresponding
  1719. * hash node property is set, for example:
  1720. *
  1721. * Input component image node structure:
  1722. *
  1723. * o image@1 (at image_noffset)
  1724. * | - data = [binary data]
  1725. * o hash@1
  1726. * |- algo = "sha1"
  1727. *
  1728. * Output component image node structure:
  1729. *
  1730. * o image@1 (at image_noffset)
  1731. * | - data = [binary data]
  1732. * o hash@1
  1733. * |- algo = "sha1"
  1734. * |- value = sha1(data)
  1735. *
  1736. * returns:
  1737. * 0 on sucess
  1738. * <0 on failure
  1739. */
  1740. int fit_image_set_hashes (void *fit, int image_noffset)
  1741. {
  1742. const void *data;
  1743. size_t size;
  1744. char *algo;
  1745. uint8_t value[FIT_MAX_HASH_LEN];
  1746. int value_len;
  1747. int noffset;
  1748. int ndepth;
  1749. /* Get image data and data length */
  1750. if (fit_image_get_data (fit, image_noffset, &data, &size)) {
  1751. printf ("Can't get image data/size\n");
  1752. return -1;
  1753. }
  1754. /* Process all hash subnodes of the component image node */
  1755. for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
  1756. (noffset >= 0) && (ndepth > 0);
  1757. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1758. if (ndepth == 1) {
  1759. /* Direct child node of the component image node */
  1760. /*
  1761. * Check subnode name, must be equal to "hash".
  1762. * Multiple hash nodes require unique unit node
  1763. * names, e.g. hash@1, hash@2, etc.
  1764. */
  1765. if (strncmp (fit_get_name(fit, noffset, NULL),
  1766. FIT_HASH_NODENAME,
  1767. strlen(FIT_HASH_NODENAME)) != 0) {
  1768. /* Not a hash subnode, skip it */
  1769. continue;
  1770. }
  1771. if (fit_image_hash_get_algo (fit, noffset, &algo)) {
  1772. printf ("Can't get hash algo property for "
  1773. "'%s' hash node in '%s' image node\n",
  1774. fit_get_name (fit, noffset, NULL),
  1775. fit_get_name (fit, image_noffset, NULL));
  1776. return -1;
  1777. }
  1778. if (calculate_hash (data, size, algo, value, &value_len)) {
  1779. printf ("Unsupported hash algorithm (%s) for "
  1780. "'%s' hash node in '%s' image node\n",
  1781. algo, fit_get_name (fit, noffset, NULL),
  1782. fit_get_name (fit, image_noffset, NULL));
  1783. return -1;
  1784. }
  1785. if (fit_image_hash_set_value (fit, noffset, value,
  1786. value_len)) {
  1787. printf ("Can't set hash value for "
  1788. "'%s' hash node in '%s' image node\n",
  1789. fit_get_name (fit, noffset, NULL),
  1790. fit_get_name (fit, image_noffset, NULL));
  1791. return -1;
  1792. }
  1793. }
  1794. }
  1795. return 0;
  1796. }
  1797. /**
  1798. * fit_image_hash_set_value - set hash value in requested has node
  1799. * @fit: pointer to the FIT format image header
  1800. * @noffset: hash node offset
  1801. * @value: hash value to be set
  1802. * @value_len: hash value length
  1803. *
  1804. * fit_image_hash_set_value() attempts to set hash value in a node at offset
  1805. * given and returns operation status to the caller.
  1806. *
  1807. * returns
  1808. * 0, on success
  1809. * -1, on failure
  1810. */
  1811. int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
  1812. int value_len)
  1813. {
  1814. int ret;
  1815. ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
  1816. if (ret) {
  1817. printf ("Can't set hash '%s' property for '%s' node (%s)\n",
  1818. FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
  1819. fdt_strerror (ret));
  1820. return -1;
  1821. }
  1822. return 0;
  1823. }
  1824. #endif /* USE_HOSTCC */
  1825. /**
  1826. * fit_image_check_hashes - verify data intergity
  1827. * @fit: pointer to the FIT format image header
  1828. * @image_noffset: component image node offset
  1829. *
  1830. * fit_image_check_hashes() goes over component image hash nodes,
  1831. * re-calculates each data hash and compares with the value stored in hash
  1832. * node.
  1833. *
  1834. * returns:
  1835. * 1, if all hashes are valid
  1836. * 0, otherwise (or on error)
  1837. */
  1838. int fit_image_check_hashes (const void *fit, int image_noffset)
  1839. {
  1840. const void *data;
  1841. size_t size;
  1842. char *algo;
  1843. uint8_t *fit_value;
  1844. int fit_value_len;
  1845. uint8_t value[FIT_MAX_HASH_LEN];
  1846. int value_len;
  1847. int noffset;
  1848. int ndepth;
  1849. char *err_msg = "";
  1850. /* Get image data and data length */
  1851. if (fit_image_get_data (fit, image_noffset, &data, &size)) {
  1852. printf ("Can't get image data/size\n");
  1853. return 0;
  1854. }
  1855. /* Process all hash subnodes of the component image node */
  1856. for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
  1857. (noffset >= 0) && (ndepth > 0);
  1858. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1859. if (ndepth == 1) {
  1860. /* Direct child node of the component image node */
  1861. /*
  1862. * Check subnode name, must be equal to "hash".
  1863. * Multiple hash nodes require unique unit node
  1864. * names, e.g. hash@1, hash@2, etc.
  1865. */
  1866. if (strncmp (fit_get_name(fit, noffset, NULL),
  1867. FIT_HASH_NODENAME,
  1868. strlen(FIT_HASH_NODENAME)) != 0)
  1869. continue;
  1870. if (fit_image_hash_get_algo (fit, noffset, &algo)) {
  1871. err_msg = "Can't get hash algo property";
  1872. goto error;
  1873. }
  1874. printf ("%s", algo);
  1875. if (fit_image_hash_get_value (fit, noffset, &fit_value,
  1876. &fit_value_len)) {
  1877. err_msg = "Can't get hash value property";
  1878. goto error;
  1879. }
  1880. if (calculate_hash (data, size, algo, value, &value_len)) {
  1881. err_msg = "Unsupported hash algorithm";
  1882. goto error;
  1883. }
  1884. if (value_len != fit_value_len) {
  1885. err_msg = "Bad hash value len";
  1886. goto error;
  1887. } else if (memcmp (value, fit_value, value_len) != 0) {
  1888. err_msg = "Bad hash value";
  1889. goto error;
  1890. }
  1891. printf ("+ ");
  1892. }
  1893. }
  1894. return 1;
  1895. error:
  1896. printf ("%s for '%s' hash node in '%s' image node\n",
  1897. err_msg, fit_get_name (fit, noffset, NULL),
  1898. fit_get_name (fit, image_noffset, NULL));
  1899. return 0;
  1900. }
  1901. /**
  1902. * fit_image_check_os - check whether image node is of a given os type
  1903. * @fit: pointer to the FIT format image header
  1904. * @noffset: component image node offset
  1905. * @os: requested image os
  1906. *
  1907. * fit_image_check_os() reads image os property and compares its numeric
  1908. * id with the requested os. Comparison result is returned to the caller.
  1909. *
  1910. * returns:
  1911. * 1 if image is of given os type
  1912. * 0 otherwise (or on error)
  1913. */
  1914. int fit_image_check_os (const void *fit, int noffset, uint8_t os)
  1915. {
  1916. uint8_t image_os;
  1917. if (fit_image_get_os (fit, noffset, &image_os))
  1918. return 0;
  1919. return (os == image_os);
  1920. }
  1921. /**
  1922. * fit_image_check_arch - check whether image node is of a given arch
  1923. * @fit: pointer to the FIT format image header
  1924. * @noffset: component image node offset
  1925. * @arch: requested imagearch
  1926. *
  1927. * fit_image_check_arch() reads image arch property and compares its numeric
  1928. * id with the requested arch. Comparison result is returned to the caller.
  1929. *
  1930. * returns:
  1931. * 1 if image is of given arch
  1932. * 0 otherwise (or on error)
  1933. */
  1934. int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
  1935. {
  1936. uint8_t image_arch;
  1937. if (fit_image_get_arch (fit, noffset, &image_arch))
  1938. return 0;
  1939. return (arch == image_arch);
  1940. }
  1941. /**
  1942. * fit_image_check_type - check whether image node is of a given type
  1943. * @fit: pointer to the FIT format image header
  1944. * @noffset: component image node offset
  1945. * @type: requested image type
  1946. *
  1947. * fit_image_check_type() reads image type property and compares its numeric
  1948. * id with the requested type. Comparison result is returned to the caller.
  1949. *
  1950. * returns:
  1951. * 1 if image is of given type
  1952. * 0 otherwise (or on error)
  1953. */
  1954. int fit_image_check_type (const void *fit, int noffset, uint8_t type)
  1955. {
  1956. uint8_t image_type;
  1957. if (fit_image_get_type (fit, noffset, &image_type))
  1958. return 0;
  1959. return (type == image_type);
  1960. }
  1961. /**
  1962. * fit_image_check_comp - check whether image node uses given compression
  1963. * @fit: pointer to the FIT format image header
  1964. * @noffset: component image node offset
  1965. * @comp: requested image compression type
  1966. *
  1967. * fit_image_check_comp() reads image compression property and compares its
  1968. * numeric id with the requested compression type. Comparison result is
  1969. * returned to the caller.
  1970. *
  1971. * returns:
  1972. * 1 if image uses requested compression
  1973. * 0 otherwise (or on error)
  1974. */
  1975. int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
  1976. {
  1977. uint8_t image_comp;
  1978. if (fit_image_get_comp (fit, noffset, &image_comp))
  1979. return 0;
  1980. return (comp == image_comp);
  1981. }
  1982. /**
  1983. * fit_check_format - sanity check FIT image format
  1984. * @fit: pointer to the FIT format image header
  1985. *
  1986. * fit_check_format() runs a basic sanity FIT image verification.
  1987. * Routine checks for mandatory properties, nodes, etc.
  1988. *
  1989. * returns:
  1990. * 1, on success
  1991. * 0, on failure
  1992. */
  1993. int fit_check_format (const void *fit)
  1994. {
  1995. /* mandatory / node 'description' property */
  1996. if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1997. debug ("Wrong FIT format: no description\n");
  1998. return 0;
  1999. }
  2000. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  2001. /* mandatory / node 'timestamp' property */
  2002. if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  2003. debug ("Wrong FIT format: no description\n");
  2004. return 0;
  2005. }
  2006. #endif
  2007. /* mandatory subimages parent '/images' node */
  2008. if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
  2009. debug ("Wrong FIT format: no images parent node\n");
  2010. return 0;
  2011. }
  2012. return 1;
  2013. }
  2014. /**
  2015. * fit_conf_get_node - get node offset for configuration of a given unit name
  2016. * @fit: pointer to the FIT format image header
  2017. * @conf_uname: configuration node unit name
  2018. *
  2019. * fit_conf_get_node() finds a configuration (withing the '/configurations'
  2020. * parant node) of a provided unit name. If configuration is found its node offset
  2021. * is returned to the caller.
  2022. *
  2023. * When NULL is provided in second argument fit_conf_get_node() will search
  2024. * for a default configuration node instead. Default configuration node unit name
  2025. * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
  2026. *
  2027. * returns:
  2028. * configuration node offset when found (>=0)
  2029. * negative number on failure (FDT_ERR_* code)
  2030. */
  2031. int fit_conf_get_node (const void *fit, const char *conf_uname)
  2032. {
  2033. int noffset, confs_noffset;
  2034. int len;
  2035. confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
  2036. if (confs_noffset < 0) {
  2037. debug ("Can't find configurations parent node '%s' (%s)\n",
  2038. FIT_CONFS_PATH, fdt_strerror (confs_noffset));
  2039. return confs_noffset;
  2040. }
  2041. if (conf_uname == NULL) {
  2042. /* get configuration unit name from the default property */
  2043. debug ("No configuration specified, trying default...\n");
  2044. conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
  2045. if (conf_uname == NULL) {
  2046. fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
  2047. return len;
  2048. }
  2049. debug ("Found default configuration: '%s'\n", conf_uname);
  2050. }
  2051. noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
  2052. if (noffset < 0) {
  2053. debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
  2054. conf_uname, fdt_strerror (noffset));
  2055. }
  2056. return noffset;
  2057. }
  2058. static int __fit_conf_get_prop_node (const void *fit, int noffset,
  2059. const char *prop_name)
  2060. {
  2061. char *uname;
  2062. int len;
  2063. /* get kernel image unit name from configuration kernel property */
  2064. uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
  2065. if (uname == NULL)
  2066. return len;
  2067. return fit_image_get_node (fit, uname);
  2068. }
  2069. /**
  2070. * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
  2071. * a given configuration
  2072. * @fit: pointer to the FIT format image header
  2073. * @noffset: configuration node offset
  2074. *
  2075. * fit_conf_get_kernel_node() retrives kernel image node unit name from
  2076. * configuration FIT_KERNEL_PROP property and translates it to the node
  2077. * offset.
  2078. *
  2079. * returns:
  2080. * image node offset when found (>=0)
  2081. * negative number on failure (FDT_ERR_* code)
  2082. */
  2083. int fit_conf_get_kernel_node (const void *fit, int noffset)
  2084. {
  2085. return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
  2086. }
  2087. /**
  2088. * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
  2089. * a given configuration
  2090. * @fit: pointer to the FIT format image header
  2091. * @noffset: configuration node offset
  2092. *
  2093. * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
  2094. * configuration FIT_KERNEL_PROP property and translates it to the node
  2095. * offset.
  2096. *
  2097. * returns:
  2098. * image node offset when found (>=0)
  2099. * negative number on failure (FDT_ERR_* code)
  2100. */
  2101. int fit_conf_get_ramdisk_node (const void *fit, int noffset)
  2102. {
  2103. return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
  2104. }
  2105. /**
  2106. * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
  2107. * a given configuration
  2108. * @fit: pointer to the FIT format image header
  2109. * @noffset: configuration node offset
  2110. *
  2111. * fit_conf_get_fdt_node() retrives fdt image node unit name from
  2112. * configuration FIT_KERNEL_PROP property and translates it to the node
  2113. * offset.
  2114. *
  2115. * returns:
  2116. * image node offset when found (>=0)
  2117. * negative number on failure (FDT_ERR_* code)
  2118. */
  2119. int fit_conf_get_fdt_node (const void *fit, int noffset)
  2120. {
  2121. return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
  2122. }
  2123. /**
  2124. * fit_conf_print - prints out the FIT configuration details
  2125. * @fit: pointer to the FIT format image header
  2126. * @conf_noffset: offset of the configuration node
  2127. * @p: pointer to prefix string
  2128. *
  2129. * fit_conf_print() lists all mandatory properies for the processed
  2130. * configuration node.
  2131. *
  2132. * returns:
  2133. * no returned results
  2134. */
  2135. void fit_conf_print (const void *fit, int noffset, const char *p)
  2136. {
  2137. char *desc;
  2138. char *uname;
  2139. int ret;
  2140. /* Mandatory properties */
  2141. ret = fit_get_desc (fit, noffset, &desc);
  2142. printf ("%s Description: ", p);
  2143. if (ret)
  2144. printf ("unavailable\n");
  2145. else
  2146. printf ("%s\n", desc);
  2147. uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
  2148. printf ("%s Kernel: ", p);
  2149. if (uname == NULL)
  2150. printf ("unavailable\n");
  2151. else
  2152. printf ("%s\n", uname);
  2153. /* Optional properties */
  2154. uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
  2155. if (uname)
  2156. printf ("%s Init Ramdisk: %s\n", p, uname);
  2157. uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
  2158. if (uname)
  2159. printf ("%s FDT: %s\n", p, uname);
  2160. }
  2161. /**
  2162. * fit_check_ramdisk - verify FIT format ramdisk subimage
  2163. * @fit_hdr: pointer to the FIT ramdisk header
  2164. * @rd_noffset: ramdisk subimage node offset within FIT image
  2165. * @arch: requested ramdisk image architecture type
  2166. * @verify: data CRC verification flag
  2167. *
  2168. * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
  2169. * specified FIT image.
  2170. *
  2171. * returns:
  2172. * 1, on success
  2173. * 0, on failure
  2174. */
  2175. #ifndef USE_HOSTCC
  2176. static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
  2177. {
  2178. fit_image_print (fit, rd_noffset, " ");
  2179. if (verify) {
  2180. puts (" Verifying Hash Integrity ... ");
  2181. if (!fit_image_check_hashes (fit, rd_noffset)) {
  2182. puts ("Bad Data Hash\n");
  2183. show_boot_progress (-125);
  2184. return 0;
  2185. }
  2186. puts ("OK\n");
  2187. }
  2188. show_boot_progress (126);
  2189. if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
  2190. !fit_image_check_arch (fit, rd_noffset, arch) ||
  2191. !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
  2192. printf ("No Linux %s Ramdisk Image\n",
  2193. genimg_get_arch_name(arch));
  2194. show_boot_progress (-126);
  2195. return 0;
  2196. }
  2197. show_boot_progress (127);
  2198. return 1;
  2199. }
  2200. #endif /* USE_HOSTCC */
  2201. #endif /* CONFIG_FIT */