kwbimage.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * (C) Copyright 2008
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include "imagetool.h"
  9. #include <image.h>
  10. #include "kwbimage.h"
  11. /*
  12. * Supported commands for configuration file
  13. */
  14. static table_entry_t kwbimage_cmds[] = {
  15. {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
  16. {CMD_NAND_ECC_MODE, "NAND_ECC_MODE", "NAND mode", },
  17. {CMD_NAND_PAGE_SIZE, "NAND_PAGE_SIZE", "NAND size", },
  18. {CMD_SATA_PIO_MODE, "SATA_PIO_MODE", "SATA mode", },
  19. {CMD_DDR_INIT_DELAY, "DDR_INIT_DELAY", "DDR init dly", },
  20. {CMD_DATA, "DATA", "Reg Write Data", },
  21. {CMD_INVALID, "", "", },
  22. };
  23. /*
  24. * Supported Boot options for configuration file
  25. */
  26. static table_entry_t kwbimage_bootops[] = {
  27. {IBR_HDR_SPI_ID, "spi", "SPI Flash", },
  28. {IBR_HDR_NAND_ID, "nand", "NAND Flash", },
  29. {IBR_HDR_SATA_ID, "sata", "Sata port", },
  30. {IBR_HDR_PEX_ID, "pex", "PCIe port", },
  31. {IBR_HDR_UART_ID, "uart", "Serial port", },
  32. {-1, "", "Invalid", },
  33. };
  34. /*
  35. * Supported NAND ecc options configuration file
  36. */
  37. static table_entry_t kwbimage_eccmodes[] = {
  38. {IBR_HDR_ECC_DEFAULT, "default", "Default mode", },
  39. {IBR_HDR_ECC_FORCED_HAMMING, "hamming", "Hamming mode", },
  40. {IBR_HDR_ECC_FORCED_RS, "rs", "RS mode", },
  41. {IBR_HDR_ECC_DISABLED, "disabled", "ECC Disabled", },
  42. {-1, "", "", },
  43. };
  44. static struct kwb_header kwbimage_header;
  45. static int datacmd_cnt = 0;
  46. static char * fname = "Unknown";
  47. static int lineno = -1;
  48. /*
  49. * Report Error if xflag is set in addition to default
  50. */
  51. static int kwbimage_check_params(struct image_tool_params *params)
  52. {
  53. if (!strlen (params->imagename)) {
  54. printf ("Error:%s - Configuration file not specified, "
  55. "it is needed for kwbimage generation\n",
  56. params->cmdname);
  57. return CFG_INVALID;
  58. }
  59. return ((params->dflag && (params->fflag || params->lflag)) ||
  60. (params->fflag && (params->dflag || params->lflag)) ||
  61. (params->lflag && (params->dflag || params->fflag)) ||
  62. (params->xflag) || !(strlen (params->imagename)));
  63. }
  64. static uint32_t check_get_hexval (char *token)
  65. {
  66. uint32_t hexval;
  67. if (!sscanf (token, "%x", &hexval)) {
  68. printf ("Error:%s[%d] - Invalid hex data(%s)\n", fname,
  69. lineno, token);
  70. exit (EXIT_FAILURE);
  71. }
  72. return hexval;
  73. }
  74. /*
  75. * Generates 8 bit checksum
  76. */
  77. static uint8_t kwbimage_checksum8 (void *start, uint32_t len, uint8_t csum)
  78. {
  79. register uint8_t sum = csum;
  80. volatile uint8_t *p = (volatile uint8_t *)start;
  81. /* check len and return zero checksum if invalid */
  82. if (!len)
  83. return 0;
  84. do {
  85. sum += *p;
  86. p++;
  87. } while (--len);
  88. return (sum);
  89. }
  90. /*
  91. * Generates 32 bit checksum
  92. */
  93. static uint32_t kwbimage_checksum32 (uint32_t *start, uint32_t len, uint32_t csum)
  94. {
  95. register uint32_t sum = csum;
  96. volatile uint32_t *p = start;
  97. /* check len and return zero checksum if invalid */
  98. if (!len)
  99. return 0;
  100. if (len % sizeof(uint32_t)) {
  101. printf ("Error:%s[%d] - length is not in multiple of %zu\n",
  102. __FUNCTION__, len, sizeof(uint32_t));
  103. return 0;
  104. }
  105. do {
  106. sum += *p;
  107. p++;
  108. len -= sizeof(uint32_t);
  109. } while (len > 0);
  110. return (sum);
  111. }
  112. static void kwbimage_check_cfgdata (char *token, enum kwbimage_cmd cmdsw,
  113. struct kwb_header *kwbhdr)
  114. {
  115. bhr_t *mhdr = &kwbhdr->kwb_hdr;
  116. extbhr_t *exthdr = &kwbhdr->kwb_exthdr;
  117. int i;
  118. switch (cmdsw) {
  119. case CMD_BOOT_FROM:
  120. i = get_table_entry_id (kwbimage_bootops,
  121. "Kwbimage boot option", token);
  122. if (i < 0)
  123. goto INVL_DATA;
  124. mhdr->blockid = i;
  125. printf ("Preparing kirkwood boot image to boot "
  126. "from %s\n", token);
  127. break;
  128. case CMD_NAND_ECC_MODE:
  129. i = get_table_entry_id (kwbimage_eccmodes,
  130. "NAND ecc mode", token);
  131. if (i < 0)
  132. goto INVL_DATA;
  133. mhdr->nandeccmode = i;
  134. printf ("Nand ECC mode = %s\n", token);
  135. break;
  136. case CMD_NAND_PAGE_SIZE:
  137. mhdr->nandpagesize =
  138. (uint16_t) check_get_hexval (token);
  139. printf ("Nand page size = 0x%x\n", mhdr->nandpagesize);
  140. break;
  141. case CMD_SATA_PIO_MODE:
  142. mhdr->satapiomode =
  143. (uint8_t) check_get_hexval (token);
  144. printf ("Sata PIO mode = 0x%x\n",
  145. mhdr->satapiomode);
  146. break;
  147. case CMD_DDR_INIT_DELAY:
  148. mhdr->ddrinitdelay =
  149. (uint16_t) check_get_hexval (token);
  150. printf ("DDR init delay = %d msec\n", mhdr->ddrinitdelay);
  151. break;
  152. case CMD_DATA:
  153. exthdr->rcfg[datacmd_cnt].raddr =
  154. check_get_hexval (token);
  155. break;
  156. case CMD_INVALID:
  157. goto INVL_DATA;
  158. default:
  159. goto INVL_DATA;
  160. }
  161. return;
  162. INVL_DATA:
  163. printf ("Error:%s[%d] - Invalid data\n", fname, lineno);
  164. exit (EXIT_FAILURE);
  165. }
  166. /*
  167. * this function sets the kwbimage header by-
  168. * 1. Abstracting input command line arguments data
  169. * 2. parses the kwbimage configuration file and update extebded header data
  170. * 3. calculates header, extended header and image checksums
  171. */
  172. static void kwdimage_set_ext_header (struct kwb_header *kwbhdr, char* name) {
  173. bhr_t *mhdr = &kwbhdr->kwb_hdr;
  174. extbhr_t *exthdr = &kwbhdr->kwb_exthdr;
  175. FILE *fd = NULL;
  176. int j;
  177. char *line = NULL;
  178. char * token, *saveptr1, *saveptr2;
  179. size_t len = 0;
  180. enum kwbimage_cmd cmd;
  181. fname = name;
  182. /* set dram register offset */
  183. exthdr->dramregsoffs = (intptr_t)&exthdr->rcfg - (intptr_t)mhdr;
  184. if ((fd = fopen (name, "r")) == 0) {
  185. printf ("Error:%s - Can't open\n", fname);
  186. exit (EXIT_FAILURE);
  187. }
  188. /* Simple kwimage.cfg file parser */
  189. lineno=0;
  190. while ((getline (&line, &len, fd)) > 0) {
  191. lineno++;
  192. token = strtok_r (line, "\r\n", &saveptr1);
  193. /* drop all lines with zero tokens (= empty lines) */
  194. if (token == NULL)
  195. continue;
  196. for (j = 0, cmd = CMD_INVALID, line = token; ; line = NULL) {
  197. token = strtok_r (line, " \t", &saveptr2);
  198. if (token == NULL)
  199. break;
  200. /* Drop all text starting with '#' as comments */
  201. if (token[0] == '#')
  202. break;
  203. /* Process rest as valid config command line */
  204. switch (j) {
  205. case CFG_COMMAND:
  206. cmd = get_table_entry_id (kwbimage_cmds,
  207. "Kwbimage command", token);
  208. if (cmd == CMD_INVALID)
  209. goto INVL_CMD;
  210. break;
  211. case CFG_DATA0:
  212. kwbimage_check_cfgdata (token, cmd, kwbhdr);
  213. break;
  214. case CFG_DATA1:
  215. if (cmd != CMD_DATA)
  216. goto INVL_CMD;
  217. exthdr->rcfg[datacmd_cnt].rdata =
  218. check_get_hexval (token);
  219. if (datacmd_cnt > KWBIMAGE_MAX_CONFIG ) {
  220. printf ("Error:%s[%d] - Found more "
  221. "than max(%zd) allowed "
  222. "data configurations\n",
  223. fname, lineno,
  224. KWBIMAGE_MAX_CONFIG);
  225. exit (EXIT_FAILURE);
  226. } else
  227. datacmd_cnt++;
  228. break;
  229. default:
  230. goto INVL_CMD;
  231. }
  232. j++;
  233. }
  234. }
  235. if (line)
  236. free (line);
  237. fclose (fd);
  238. return;
  239. /*
  240. * Invalid Command error reporring
  241. *
  242. * command CMD_DATA needs three strings on a line
  243. * whereas other commands need only two.
  244. *
  245. * if more than two/three (as per command type) are observed,
  246. * then error will be reported
  247. */
  248. INVL_CMD:
  249. printf ("Error:%s[%d] - Invalid command\n", fname, lineno);
  250. exit (EXIT_FAILURE);
  251. }
  252. static void kwbimage_set_header (void *ptr, struct stat *sbuf, int ifd,
  253. struct image_tool_params *params)
  254. {
  255. struct kwb_header *hdr = (struct kwb_header *)ptr;
  256. bhr_t *mhdr = &hdr->kwb_hdr;
  257. extbhr_t *exthdr = &hdr->kwb_exthdr;
  258. uint32_t checksum;
  259. int size;
  260. /* Build and add image checksum header */
  261. checksum = kwbimage_checksum32 ((uint32_t *)ptr, sbuf->st_size, 0);
  262. size = write (ifd, &checksum, sizeof(uint32_t));
  263. if (size != sizeof(uint32_t)) {
  264. printf ("Error:%s - Checksum write %d bytes %s\n",
  265. params->cmdname, size, params->imagefile);
  266. exit (EXIT_FAILURE);
  267. }
  268. sbuf->st_size += sizeof(uint32_t);
  269. mhdr->blocksize = sbuf->st_size - sizeof(struct kwb_header);
  270. mhdr->srcaddr = sizeof(struct kwb_header);
  271. mhdr->destaddr= params->addr;
  272. mhdr->execaddr =params->ep;
  273. mhdr->ext = 0x1; /* header extension appended */
  274. kwdimage_set_ext_header (hdr, params->imagename);
  275. /* calculate checksums */
  276. mhdr->checkSum = kwbimage_checksum8 ((void *)mhdr, sizeof(bhr_t), 0);
  277. exthdr->checkSum = kwbimage_checksum8 ((void *)exthdr,
  278. sizeof(extbhr_t), 0);
  279. }
  280. static int kwbimage_verify_header (unsigned char *ptr, int image_size,
  281. struct image_tool_params *params)
  282. {
  283. struct kwb_header *hdr = (struct kwb_header *)ptr;
  284. bhr_t *mhdr = &hdr->kwb_hdr;
  285. extbhr_t *exthdr = &hdr->kwb_exthdr;
  286. uint8_t calc_hdrcsum;
  287. uint8_t calc_exthdrcsum;
  288. calc_hdrcsum = kwbimage_checksum8 ((void *)mhdr,
  289. sizeof(bhr_t) - sizeof(uint8_t), 0);
  290. if (calc_hdrcsum != mhdr->checkSum)
  291. return -FDT_ERR_BADSTRUCTURE; /* mhdr csum not matched */
  292. calc_exthdrcsum = kwbimage_checksum8 ((void *)exthdr,
  293. sizeof(extbhr_t) - sizeof(uint8_t), 0);
  294. if (calc_exthdrcsum != exthdr->checkSum)
  295. return -FDT_ERR_BADSTRUCTURE; /* exthdr csum not matched */
  296. return 0;
  297. }
  298. static void kwbimage_print_header (const void *ptr)
  299. {
  300. struct kwb_header *hdr = (struct kwb_header *) ptr;
  301. bhr_t *mhdr = &hdr->kwb_hdr;
  302. char *name = get_table_entry_name (kwbimage_bootops,
  303. "Kwbimage boot option",
  304. (int) mhdr->blockid);
  305. printf ("Image Type: Kirkwood Boot from %s Image\n", name);
  306. printf ("Data Size: ");
  307. genimg_print_size (mhdr->blocksize - sizeof(uint32_t));
  308. printf ("Load Address: %08x\n", mhdr->destaddr);
  309. printf ("Entry Point: %08x\n", mhdr->execaddr);
  310. }
  311. static int kwbimage_check_image_types (uint8_t type)
  312. {
  313. if (type == IH_TYPE_KWBIMAGE)
  314. return EXIT_SUCCESS;
  315. else
  316. return EXIT_FAILURE;
  317. }
  318. /*
  319. * kwbimage type parameters definition
  320. */
  321. static struct image_type_params kwbimage_params = {
  322. .name = "Kirkwood Boot Image support",
  323. .header_size = sizeof(struct kwb_header),
  324. .hdr = (void*)&kwbimage_header,
  325. .check_image_type = kwbimage_check_image_types,
  326. .verify_header = kwbimage_verify_header,
  327. .print_header = kwbimage_print_header,
  328. .set_header = kwbimage_set_header,
  329. .check_params = kwbimage_check_params,
  330. };
  331. void init_kwb_image_type (void)
  332. {
  333. register_image_type(&kwbimage_params);
  334. }