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.
 
 
 
 
 
 

341 lines
7.0 KiB

  1. /*
  2. * (C) Copyright 2014
  3. * Andreas Bießmann <andreas@biessmann.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include "imagetool.h"
  8. #include "mkimage.h"
  9. #include <image.h>
  10. #define pr_err(fmt, args...) fprintf(stderr, "atmelimage Error: " fmt, ##args)
  11. static int atmel_check_image_type(uint8_t type)
  12. {
  13. if (type == IH_TYPE_ATMELIMAGE)
  14. return EXIT_SUCCESS;
  15. else
  16. return EXIT_FAILURE;
  17. }
  18. static uint32_t nand_pmecc_header[52];
  19. /*
  20. * A helper struct for parsing the mkimage -n parameter
  21. *
  22. * Keep in same order as the configs array!
  23. */
  24. static struct pmecc_config {
  25. int use_pmecc;
  26. int sector_per_page;
  27. int spare_size;
  28. int ecc_bits;
  29. int sector_size;
  30. int ecc_offset;
  31. } pmecc;
  32. /*
  33. * Strings used for configure the PMECC header via -n mkimage switch
  34. *
  35. * We estimate a coma separated list of key=value pairs. The mkimage -n
  36. * parameter argument should not contain any whitespace.
  37. *
  38. * Keep in same order as struct pmecc_config!
  39. */
  40. static const char * const configs[] = {
  41. "usePmecc",
  42. "sectorPerPage",
  43. "spareSize",
  44. "eccBits",
  45. "sectorSize",
  46. "eccOffset"
  47. };
  48. static int atmel_find_pmecc_parameter_in_token(const char *token)
  49. {
  50. size_t pos;
  51. char *param;
  52. debug("token: '%s'\n", token);
  53. for (pos = 0; pos < ARRAY_SIZE(configs); pos++) {
  54. if (strncmp(token, configs[pos], strlen(configs[pos])) == 0) {
  55. param = strstr(token, "=");
  56. if (!param)
  57. goto err;
  58. param++;
  59. debug("\t%s parameter: '%s'\n", configs[pos], param);
  60. switch (pos) {
  61. case 0:
  62. pmecc.use_pmecc = strtol(param, NULL, 10);
  63. return EXIT_SUCCESS;
  64. case 1:
  65. pmecc.sector_per_page = strtol(param, NULL, 10);
  66. return EXIT_SUCCESS;
  67. case 2:
  68. pmecc.spare_size = strtol(param, NULL, 10);
  69. return EXIT_SUCCESS;
  70. case 3:
  71. pmecc.ecc_bits = strtol(param, NULL, 10);
  72. return EXIT_SUCCESS;
  73. case 4:
  74. pmecc.sector_size = strtol(param, NULL, 10);
  75. return EXIT_SUCCESS;
  76. case 5:
  77. pmecc.ecc_offset = strtol(param, NULL, 10);
  78. return EXIT_SUCCESS;
  79. }
  80. }
  81. }
  82. err:
  83. pr_err("Could not find parameter in token '%s'\n", token);
  84. return EXIT_FAILURE;
  85. }
  86. static int atmel_parse_pmecc_params(char *txt)
  87. {
  88. char *token;
  89. token = strtok(txt, ",");
  90. while (token != NULL) {
  91. if (atmel_find_pmecc_parameter_in_token(token))
  92. return EXIT_FAILURE;
  93. token = strtok(NULL, ",");
  94. }
  95. return EXIT_SUCCESS;
  96. }
  97. static int atmel_verify_header(unsigned char *ptr, int image_size,
  98. struct image_tool_params *params)
  99. {
  100. uint32_t *ints = (uint32_t *)ptr;
  101. size_t pos;
  102. size_t size = image_size;
  103. /* check if we have an PMECC header attached */
  104. for (pos = 0; pos < ARRAY_SIZE(nand_pmecc_header); pos++)
  105. if (ints[pos] >> 28 != 0xC)
  106. break;
  107. if (pos == ARRAY_SIZE(nand_pmecc_header)) {
  108. ints += ARRAY_SIZE(nand_pmecc_header);
  109. size -= sizeof(nand_pmecc_header);
  110. }
  111. /* check the seven interrupt vectors of binary */
  112. for (pos = 0; pos < 7; pos++) {
  113. debug("atmelimage: interrupt vector #%zu is 0x%08X\n", pos+1,
  114. ints[pos]);
  115. /*
  116. * all vectors except the 6'th one must contain valid
  117. * LDR or B Opcode
  118. */
  119. if (pos == 5)
  120. /* 6'th vector has image size set, check later */
  121. continue;
  122. if ((ints[pos] & 0xff000000) == 0xea000000)
  123. /* valid B Opcode */
  124. continue;
  125. if ((ints[pos] & 0xfffff000) == 0xe59ff000)
  126. /* valid LDR (I=0, P=1, U=1, B=0, W=0, L=1) */
  127. continue;
  128. /* ouch, one of the checks has missed ... */
  129. return 1;
  130. }
  131. return ints[5] != cpu_to_le32(size);
  132. }
  133. static void atmel_print_pmecc_header(const uint32_t word)
  134. {
  135. int val;
  136. printf("\t\tPMECC header\n");
  137. printf("\t\t====================\n");
  138. val = (word >> 18) & 0x1ff;
  139. printf("\t\teccOffset: %9i\n", val);
  140. val = (((word >> 16) & 0x3) == 0) ? 512 : 1024;
  141. printf("\t\tsectorSize: %8i\n", val);
  142. if (((word >> 13) & 0x7) <= 2)
  143. val = (2 << ((word >> 13) & 0x7));
  144. else
  145. val = (12 << (((word >> 13) & 0x7) - 3));
  146. printf("\t\teccBitReq: %9i\n", val);
  147. val = (word >> 4) & 0x1ff;
  148. printf("\t\tspareSize: %9i\n", val);
  149. val = (1 << ((word >> 1) & 0x3));
  150. printf("\t\tnbSectorPerPage: %3i\n", val);
  151. printf("\t\tusePmecc: %10i\n", word & 0x1);
  152. printf("\t\t====================\n");
  153. }
  154. static void atmel_print_header(const void *ptr)
  155. {
  156. uint32_t *ints = (uint32_t *)ptr;
  157. size_t pos;
  158. /* check if we have an PMECC header attached */
  159. for (pos = 0; pos < ARRAY_SIZE(nand_pmecc_header); pos++)
  160. if (ints[pos] >> 28 != 0xC)
  161. break;
  162. if (pos == ARRAY_SIZE(nand_pmecc_header)) {
  163. printf("Image Type:\tATMEL ROM-Boot Image with PMECC Header\n");
  164. atmel_print_pmecc_header(ints[0]);
  165. pos += 5;
  166. } else {
  167. printf("Image Type:\tATMEL ROM-Boot Image without PMECC Header\n");
  168. pos = 5;
  169. }
  170. printf("\t\t6'th vector has %u set\n", le32_to_cpu(ints[pos]));
  171. }
  172. static void atmel_set_header(void *ptr, struct stat *sbuf, int ifd,
  173. struct image_tool_params *params)
  174. {
  175. /* just save the image size into 6'th interrupt vector */
  176. uint32_t *ints = (uint32_t *)ptr;
  177. size_t cnt;
  178. size_t pos = 5;
  179. size_t size = sbuf->st_size;
  180. for (cnt = 0; cnt < ARRAY_SIZE(nand_pmecc_header); cnt++)
  181. if (ints[cnt] >> 28 != 0xC)
  182. break;
  183. if (cnt == ARRAY_SIZE(nand_pmecc_header)) {
  184. pos += ARRAY_SIZE(nand_pmecc_header);
  185. size -= sizeof(nand_pmecc_header);
  186. }
  187. ints[pos] = cpu_to_le32(size);
  188. }
  189. static int atmel_check_params(struct image_tool_params *params)
  190. {
  191. if (strlen(params->imagename) > 0)
  192. if (atmel_parse_pmecc_params(params->imagename))
  193. return EXIT_FAILURE;
  194. return !(!params->eflag &&
  195. !params->fflag &&
  196. !params->xflag &&
  197. ((params->dflag && !params->lflag) ||
  198. (params->lflag && !params->dflag)));
  199. }
  200. static int atmel_vrec_header(struct image_tool_params *params,
  201. struct image_type_params *tparams)
  202. {
  203. uint32_t tmp;
  204. size_t pos;
  205. if (strlen(params->imagename) == 0)
  206. return EXIT_SUCCESS;
  207. tmp = 0xC << 28;
  208. tmp |= (pmecc.ecc_offset & 0x1ff) << 18;
  209. switch (pmecc.sector_size) {
  210. case 512:
  211. tmp |= 0 << 16;
  212. break;
  213. case 1024:
  214. tmp |= 1 << 16;
  215. break;
  216. default:
  217. pr_err("Wrong sectorSize (%i) for PMECC header\n",
  218. pmecc.sector_size);
  219. return EXIT_FAILURE;
  220. }
  221. switch (pmecc.ecc_bits) {
  222. case 2:
  223. tmp |= 0 << 13;
  224. break;
  225. case 4:
  226. tmp |= 1 << 13;
  227. break;
  228. case 8:
  229. tmp |= 2 << 13;
  230. break;
  231. case 12:
  232. tmp |= 3 << 13;
  233. break;
  234. case 24:
  235. tmp |= 4 << 13;
  236. break;
  237. default:
  238. pr_err("Wrong eccBits (%i) for PMECC header\n",
  239. pmecc.ecc_bits);
  240. return EXIT_FAILURE;
  241. }
  242. tmp |= (pmecc.spare_size & 0x1ff) << 4;
  243. switch (pmecc.sector_per_page) {
  244. case 1:
  245. tmp |= 0 << 1;
  246. break;
  247. case 2:
  248. tmp |= 1 << 1;
  249. break;
  250. case 4:
  251. tmp |= 2 << 1;
  252. break;
  253. case 8:
  254. tmp |= 3 << 1;
  255. break;
  256. default:
  257. pr_err("Wrong sectorPerPage (%i) for PMECC header\n",
  258. pmecc.sector_per_page);
  259. return EXIT_FAILURE;
  260. }
  261. if (pmecc.use_pmecc)
  262. tmp |= 1;
  263. for (pos = 0; pos < ARRAY_SIZE(nand_pmecc_header); pos++)
  264. nand_pmecc_header[pos] = tmp;
  265. debug("PMECC header filled 52 times with 0x%08X\n", tmp);
  266. tparams->header_size = sizeof(nand_pmecc_header);
  267. tparams->hdr = nand_pmecc_header;
  268. return EXIT_SUCCESS;
  269. }
  270. U_BOOT_IMAGE_TYPE(
  271. atmelimage,
  272. "ATMEL ROM-Boot Image support",
  273. 0,
  274. NULL,
  275. atmel_check_params,
  276. atmel_verify_header,
  277. atmel_print_header,
  278. atmel_set_header,
  279. NULL,
  280. atmel_check_image_type,
  281. NULL,
  282. atmel_vrec_header
  283. );