fb_getvar.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #include <common.h>
  6. #include <env.h>
  7. #include <fastboot.h>
  8. #include <fastboot-internal.h>
  9. #include <fb_mmc.h>
  10. #include <fb_nand.h>
  11. #include <fs.h>
  12. #include <part.h>
  13. #include <version.h>
  14. static void getvar_version(char *var_parameter, char *response);
  15. static void getvar_version_bootloader(char *var_parameter, char *response);
  16. static void getvar_downloadsize(char *var_parameter, char *response);
  17. static void getvar_serialno(char *var_parameter, char *response);
  18. static void getvar_version_baseband(char *var_parameter, char *response);
  19. static void getvar_product(char *var_parameter, char *response);
  20. static void getvar_platform(char *var_parameter, char *response);
  21. static void getvar_current_slot(char *var_parameter, char *response);
  22. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  23. static void getvar_has_slot(char *var_parameter, char *response);
  24. #endif
  25. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  26. static void getvar_partition_type(char *part_name, char *response);
  27. #endif
  28. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  29. static void getvar_partition_size(char *part_name, char *response);
  30. #endif
  31. static void getvar_is_userspace(char *var_parameter, char *response);
  32. static void getvar_logical_block_size(char *var_parameter, char *response);
  33. static const struct {
  34. const char *variable;
  35. void (*dispatch)(char *var_parameter, char *response);
  36. } getvar_dispatch[] = {
  37. {
  38. .variable = "version",
  39. .dispatch = getvar_version
  40. }, {
  41. .variable = "version-bootloader",
  42. .dispatch = getvar_version_bootloader
  43. }, {
  44. .variable = "downloadsize",
  45. .dispatch = getvar_downloadsize
  46. }, {
  47. .variable = "max-download-size",
  48. .dispatch = getvar_downloadsize
  49. }, {
  50. .variable = "serialno",
  51. .dispatch = getvar_serialno
  52. }, {
  53. .variable = "version-baseband",
  54. .dispatch = getvar_version_baseband
  55. }, {
  56. .variable = "product",
  57. .dispatch = getvar_product
  58. }, {
  59. .variable = "platform",
  60. .dispatch = getvar_platform
  61. }, {
  62. .variable = "current-slot",
  63. .dispatch = getvar_current_slot
  64. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  65. }, {
  66. .variable = "has-slot",
  67. .dispatch = getvar_has_slot
  68. #endif
  69. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  70. }, {
  71. .variable = "partition-type",
  72. .dispatch = getvar_partition_type
  73. #endif
  74. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  75. }, {
  76. .variable = "partition-size",
  77. .dispatch = getvar_partition_size
  78. #endif
  79. }, {
  80. .variable = "is-userspace",
  81. .dispatch = getvar_is_userspace
  82. }, {
  83. .variable = "logical-block-size",
  84. .dispatch = getvar_logical_block_size
  85. }
  86. };
  87. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  88. /**
  89. * Get partition number and size for any storage type.
  90. *
  91. * Can be used to check if partition with specified name exists.
  92. *
  93. * If error occurs, this function guarantees to fill @p response with fail
  94. * string. @p response can be rewritten in caller, if needed.
  95. *
  96. * @param[in] part_name Info for which partition name to look for
  97. * @param[in,out] response Pointer to fastboot response buffer
  98. * @param[out] size If not NULL, will contain partition size
  99. * @return Partition number or negative value on error
  100. */
  101. static int getvar_get_part_info(const char *part_name, char *response,
  102. size_t *size)
  103. {
  104. int r;
  105. # if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  106. struct blk_desc *dev_desc;
  107. struct disk_partition part_info;
  108. r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
  109. response);
  110. if (r >= 0 && size)
  111. *size = part_info.size * part_info.blksz;
  112. # elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
  113. struct part_info *part_info;
  114. r = fastboot_nand_get_part_info(part_name, &part_info, response);
  115. if (r >= 0 && size)
  116. *size = part_info->size;
  117. # else
  118. fastboot_fail("this storage is not supported in bootloader", response);
  119. r = -ENODEV;
  120. # endif
  121. return r;
  122. }
  123. #endif
  124. static void getvar_version(char *var_parameter, char *response)
  125. {
  126. fastboot_okay(FASTBOOT_VERSION, response);
  127. }
  128. static void getvar_version_bootloader(char *var_parameter, char *response)
  129. {
  130. fastboot_okay(U_BOOT_VERSION, response);
  131. }
  132. static void getvar_downloadsize(char *var_parameter, char *response)
  133. {
  134. fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
  135. }
  136. static void getvar_serialno(char *var_parameter, char *response)
  137. {
  138. const char *tmp = env_get("serial#");
  139. if (tmp)
  140. fastboot_okay(tmp, response);
  141. else
  142. fastboot_fail("Value not set", response);
  143. }
  144. static void getvar_version_baseband(char *var_parameter, char *response)
  145. {
  146. fastboot_okay("N/A", response);
  147. }
  148. static void getvar_product(char *var_parameter, char *response)
  149. {
  150. const char *board = env_get("board");
  151. if (board)
  152. fastboot_okay(board, response);
  153. else
  154. fastboot_fail("Board not set", response);
  155. }
  156. static void getvar_platform(char *var_parameter, char *response)
  157. {
  158. const char *p = env_get("platform");
  159. if (p)
  160. fastboot_okay(p, response);
  161. else
  162. fastboot_fail("platform not set", response);
  163. }
  164. static void getvar_current_slot(char *var_parameter, char *response)
  165. {
  166. /* A/B not implemented, for now always return "a" */
  167. fastboot_okay("a", response);
  168. }
  169. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  170. static void getvar_has_slot(char *part_name, char *response)
  171. {
  172. char part_name_wslot[PART_NAME_LEN];
  173. size_t len;
  174. int r;
  175. if (!part_name || part_name[0] == '\0')
  176. goto fail;
  177. /* part_name_wslot = part_name + "_a" */
  178. len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
  179. if (len > PART_NAME_LEN - 3)
  180. goto fail;
  181. strcat(part_name_wslot, "_a");
  182. r = getvar_get_part_info(part_name_wslot, response, NULL);
  183. if (r >= 0) {
  184. fastboot_okay("yes", response); /* part exists and slotted */
  185. return;
  186. }
  187. r = getvar_get_part_info(part_name, response, NULL);
  188. if (r >= 0)
  189. fastboot_okay("no", response); /* part exists but not slotted */
  190. /* At this point response is filled with okay or fail string */
  191. return;
  192. fail:
  193. fastboot_fail("invalid partition name", response);
  194. }
  195. #endif
  196. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  197. static void getvar_partition_type(char *part_name, char *response)
  198. {
  199. int r;
  200. struct blk_desc *dev_desc;
  201. struct disk_partition part_info;
  202. r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
  203. response);
  204. if (r >= 0) {
  205. r = fs_set_blk_dev_with_part(dev_desc, r);
  206. if (r < 0)
  207. fastboot_fail("failed to set partition", response);
  208. else
  209. fastboot_okay(fs_get_type_name(), response);
  210. }
  211. }
  212. #endif
  213. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  214. static void getvar_partition_size(char *part_name, char *response)
  215. {
  216. int r;
  217. size_t size;
  218. r = getvar_get_part_info(part_name, response, &size);
  219. if (r >= 0)
  220. fastboot_response("OKAY", response, "0x%016zx", size);
  221. }
  222. #endif
  223. static void getvar_is_userspace(char *var_parameter, char *response)
  224. {
  225. fastboot_okay("no", response);
  226. }
  227. static void getvar_logical_block_size(char *var_parameter, char *response)
  228. {
  229. struct blk_desc *dev_desc;
  230. dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  231. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  232. pr_err("invalid mmc device\n");
  233. fastboot_fail("invalid mmc device", response);
  234. return;
  235. }
  236. fastboot_response("OKAY", response, "0x%08lx", dev_desc->blksz);
  237. }
  238. /**
  239. * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
  240. *
  241. * @cmd_parameter: Pointer to command parameter
  242. * @response: Pointer to fastboot response buffer
  243. *
  244. * Look up cmd_parameter first as an environment variable of the form
  245. * fastboot.<cmd_parameter>, if that exists return use its value to set
  246. * response.
  247. *
  248. * Otherwise lookup the name of variable and execute the appropriate
  249. * function to return the requested value.
  250. */
  251. void fastboot_getvar(char *cmd_parameter, char *response)
  252. {
  253. if (!cmd_parameter) {
  254. fastboot_fail("missing var", response);
  255. } else {
  256. #define FASTBOOT_ENV_PREFIX "fastboot."
  257. int i;
  258. char *var_parameter = cmd_parameter;
  259. char envstr[FASTBOOT_RESPONSE_LEN];
  260. const char *s;
  261. snprintf(envstr, sizeof(envstr) - 1,
  262. FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
  263. s = env_get(envstr);
  264. if (s) {
  265. fastboot_response("OKAY", response, "%s", s);
  266. return;
  267. }
  268. strsep(&var_parameter, ":");
  269. for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
  270. if (!strcmp(getvar_dispatch[i].variable,
  271. cmd_parameter)) {
  272. getvar_dispatch[i].dispatch(var_parameter,
  273. response);
  274. return;
  275. }
  276. }
  277. pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
  278. fastboot_fail("Variable not implemented", response);
  279. }
  280. }