fb_getvar.c 7.7 KB

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