Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

261 lines
6.1 KiB

  1. /*
  2. * (C) Copyright 2011
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  4. *
  5. * Based on:
  6. * (C) Copyright 2009
  7. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  8. *
  9. * (C) Copyright 2008
  10. * Marvell Semiconductor <www.marvell.com>
  11. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  12. *
  13. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #include "imagetool.h"
  16. #include <image.h>
  17. #include "ublimage.h"
  18. /*
  19. * Supported commands for configuration file
  20. */
  21. static table_entry_t ublimage_cmds[] = {
  22. {CMD_BOOT_MODE, "MODE", "UBL special modes", },
  23. {CMD_ENTRY, "ENTRY", "Entry point addr for bootloader", },
  24. {CMD_PAGE, "PAGES",
  25. "number of pages (size of bootloader)", },
  26. {CMD_ST_BLOCK, "START_BLOCK",
  27. "block number where bootloader is present", },
  28. {CMD_ST_PAGE, "START_PAGE",
  29. "page number where bootloader is present", },
  30. {CMD_LD_ADDR, "LD_ADDR",
  31. "load addr", },
  32. {-1, "", "", },
  33. };
  34. /*
  35. * Supported Boot options for configuration file
  36. * this is needed to set the correct flash offset
  37. */
  38. static table_entry_t ublimage_bootops[] = {
  39. {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
  40. {-1, "", "Invalid", },
  41. };
  42. static struct ubl_header ublimage_header;
  43. static uint32_t get_cfg_value(char *token, char *name, int linenr)
  44. {
  45. char *endptr;
  46. uint32_t value;
  47. errno = 0;
  48. value = strtoul(token, &endptr, 16);
  49. if (errno || (token == endptr)) {
  50. fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
  51. name, linenr, token);
  52. exit(EXIT_FAILURE);
  53. }
  54. return value;
  55. }
  56. static void print_hdr(struct ubl_header *ubl_hdr)
  57. {
  58. printf("Image Type : Davinci UBL Boot Image\n");
  59. printf("UBL magic : %08x\n", ubl_hdr->magic);
  60. printf("Entry Point: %08x\n", ubl_hdr->entry);
  61. printf("nr of pages: %08x\n", ubl_hdr->pages);
  62. printf("start block: %08x\n", ubl_hdr->block);
  63. printf("start page : %08x\n", ubl_hdr->page);
  64. }
  65. static void parse_cfg_cmd(struct ubl_header *ublhdr, int32_t cmd, char *token,
  66. char *name, int lineno, int fld, int dcd_len)
  67. {
  68. static int cmd_ver_first = ~0;
  69. switch (cmd) {
  70. case CMD_BOOT_MODE:
  71. ublhdr->magic = get_table_entry_id(ublimage_bootops,
  72. "ublimage special boot mode", token);
  73. if (ublhdr->magic == -1) {
  74. fprintf(stderr, "Error: %s[%d] -Invalid boot mode"
  75. "(%s)\n", name, lineno, token);
  76. exit(EXIT_FAILURE);
  77. }
  78. ublhdr->magic += UBL_MAGIC_BASE;
  79. if (unlikely(cmd_ver_first != 1))
  80. cmd_ver_first = 0;
  81. break;
  82. case CMD_ENTRY:
  83. ublhdr->entry = get_cfg_value(token, name, lineno);
  84. break;
  85. case CMD_PAGE:
  86. ublhdr->pages = get_cfg_value(token, name, lineno);
  87. break;
  88. case CMD_ST_BLOCK:
  89. ublhdr->block = get_cfg_value(token, name, lineno);
  90. break;
  91. case CMD_ST_PAGE:
  92. ublhdr->page = get_cfg_value(token, name, lineno);
  93. break;
  94. case CMD_LD_ADDR:
  95. ublhdr->pll_m = get_cfg_value(token, name, lineno);
  96. break;
  97. }
  98. }
  99. static void parse_cfg_fld(struct ubl_header *ublhdr, int32_t *cmd,
  100. char *token, char *name, int lineno, int fld, int *dcd_len)
  101. {
  102. switch (fld) {
  103. case CFG_COMMAND:
  104. *cmd = get_table_entry_id(ublimage_cmds,
  105. "ublimage commands", token);
  106. if (*cmd < 0) {
  107. fprintf(stderr, "Error: %s[%d] - Invalid command"
  108. "(%s)\n", name, lineno, token);
  109. exit(EXIT_FAILURE);
  110. }
  111. break;
  112. case CFG_REG_VALUE:
  113. parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
  120. {
  121. FILE *fd = NULL;
  122. char *line = NULL;
  123. char *token, *saveptr1, *saveptr2;
  124. int lineno = 0;
  125. int i;
  126. char *ptr = (char *)ublhdr;
  127. int fld;
  128. size_t len;
  129. int dcd_len = 0;
  130. int32_t cmd;
  131. int ublhdrlen = sizeof(struct ubl_header);
  132. fd = fopen(name, "r");
  133. if (fd == 0) {
  134. fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
  135. exit(EXIT_FAILURE);
  136. }
  137. /* Fill header with 0xff */
  138. for (i = 0; i < ublhdrlen; i++) {
  139. *ptr = 0xff;
  140. ptr++;
  141. }
  142. /*
  143. * Very simple parsing, line starting with # are comments
  144. * and are dropped
  145. */
  146. while ((getline(&line, &len, fd)) > 0) {
  147. lineno++;
  148. token = strtok_r(line, "\r\n", &saveptr1);
  149. if (token == NULL)
  150. continue;
  151. /* Check inside the single line */
  152. for (fld = CFG_COMMAND, cmd = CMD_INVALID,
  153. line = token; ; line = NULL, fld++) {
  154. token = strtok_r(line, " \t", &saveptr2);
  155. if (token == NULL)
  156. break;
  157. /* Drop all text starting with '#' as comments */
  158. if (token[0] == '#')
  159. break;
  160. parse_cfg_fld(ublhdr, &cmd, token, name,
  161. lineno, fld, &dcd_len);
  162. }
  163. }
  164. fclose(fd);
  165. return dcd_len;
  166. }
  167. static int ublimage_check_image_types(uint8_t type)
  168. {
  169. if (type == IH_TYPE_UBLIMAGE)
  170. return EXIT_SUCCESS;
  171. else
  172. return EXIT_FAILURE;
  173. }
  174. static int ublimage_verify_header(unsigned char *ptr, int image_size,
  175. struct image_tool_params *params)
  176. {
  177. struct ubl_header *ubl_hdr = (struct ubl_header *)ptr;
  178. if ((ubl_hdr->magic & 0xFFFFFF00) != UBL_MAGIC_BASE)
  179. return -1;
  180. return 0;
  181. }
  182. static void ublimage_print_header(const void *ptr)
  183. {
  184. struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
  185. print_hdr(ubl_hdr);
  186. }
  187. static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  188. struct image_tool_params *params)
  189. {
  190. struct ubl_header *ublhdr = (struct ubl_header *)ptr;
  191. /* Parse configuration file */
  192. parse_cfg_file(ublhdr, params->imagename);
  193. }
  194. int ublimage_check_params(struct image_tool_params *params)
  195. {
  196. if (!params)
  197. return CFG_INVALID;
  198. if (!strlen(params->imagename)) {
  199. fprintf(stderr, "Error: %s - Configuration file not"
  200. "specified, it is needed for ublimage generation\n",
  201. params->cmdname);
  202. return CFG_INVALID;
  203. }
  204. /*
  205. * Check parameters:
  206. * XIP is not allowed and verify that incompatible
  207. * parameters are not sent at the same time
  208. * For example, if list is required a data image must not be provided
  209. */
  210. return (params->dflag && (params->fflag || params->lflag)) ||
  211. (params->fflag && (params->dflag || params->lflag)) ||
  212. (params->lflag && (params->dflag || params->fflag)) ||
  213. (params->xflag) || !(strlen(params->imagename));
  214. }
  215. /*
  216. * ublimage parameters
  217. */
  218. U_BOOT_IMAGE_TYPE(
  219. ublimage,
  220. "Davinci UBL boot support",
  221. sizeof(struct ubl_header),
  222. (void *)&ublimage_header,
  223. ublimage_check_params,
  224. ublimage_verify_header,
  225. ublimage_print_header,
  226. ublimage_set_header,
  227. NULL,
  228. ublimage_check_image_types,
  229. NULL,
  230. NULL
  231. );