fb_getvar.c 7.6 KB

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