fb_getvar.c 7.4 KB

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