fb_getvar.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #include <common.h>
  6. #include <fastboot.h>
  7. #include <fastboot-internal.h>
  8. #include <fb_mmc.h>
  9. #include <fb_nand.h>
  10. #include <fs.h>
  11. #include <version.h>
  12. static void getvar_version(char *var_parameter, char *response);
  13. static void getvar_bootloader_version(char *var_parameter, char *response);
  14. static void getvar_downloadsize(char *var_parameter, char *response);
  15. static void getvar_serialno(char *var_parameter, char *response);
  16. static void getvar_version_baseband(char *var_parameter, char *response);
  17. static void getvar_product(char *var_parameter, char *response);
  18. static void getvar_current_slot(char *var_parameter, char *response);
  19. static void getvar_slot_suffixes(char *var_parameter, char *response);
  20. static void getvar_has_slot(char *var_parameter, char *response);
  21. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  22. static void getvar_partition_type(char *part_name, char *response);
  23. #endif
  24. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  25. static void getvar_partition_size(char *part_name, char *response);
  26. #endif
  27. static const struct {
  28. const char *variable;
  29. void (*dispatch)(char *var_parameter, char *response);
  30. } getvar_dispatch[] = {
  31. {
  32. .variable = "version",
  33. .dispatch = getvar_version
  34. }, {
  35. .variable = "bootloader-version",
  36. .dispatch = getvar_bootloader_version
  37. }, {
  38. .variable = "version-bootloader",
  39. .dispatch = getvar_bootloader_version
  40. }, {
  41. .variable = "downloadsize",
  42. .dispatch = getvar_downloadsize
  43. }, {
  44. .variable = "max-download-size",
  45. .dispatch = getvar_downloadsize
  46. }, {
  47. .variable = "serialno",
  48. .dispatch = getvar_serialno
  49. }, {
  50. .variable = "version-baseband",
  51. .dispatch = getvar_version_baseband
  52. }, {
  53. .variable = "product",
  54. .dispatch = getvar_product
  55. }, {
  56. .variable = "current-slot",
  57. .dispatch = getvar_current_slot
  58. }, {
  59. .variable = "slot-suffixes",
  60. .dispatch = getvar_slot_suffixes
  61. }, {
  62. .variable = "has_slot",
  63. .dispatch = getvar_has_slot
  64. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  65. }, {
  66. .variable = "partition-type",
  67. .dispatch = getvar_partition_type
  68. #endif
  69. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  70. }, {
  71. .variable = "partition-size",
  72. .dispatch = getvar_partition_size
  73. #endif
  74. }
  75. };
  76. static void getvar_version(char *var_parameter, char *response)
  77. {
  78. fastboot_okay(FASTBOOT_VERSION, response);
  79. }
  80. static void getvar_bootloader_version(char *var_parameter, char *response)
  81. {
  82. fastboot_okay(U_BOOT_VERSION, response);
  83. }
  84. static void getvar_downloadsize(char *var_parameter, char *response)
  85. {
  86. fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
  87. }
  88. static void getvar_serialno(char *var_parameter, char *response)
  89. {
  90. const char *tmp = env_get("serial#");
  91. if (tmp)
  92. fastboot_okay(tmp, response);
  93. else
  94. fastboot_fail("Value not set", response);
  95. }
  96. static void getvar_version_baseband(char *var_parameter, char *response)
  97. {
  98. fastboot_okay("N/A", response);
  99. }
  100. static void getvar_product(char *var_parameter, char *response)
  101. {
  102. const char *board = env_get("board");
  103. if (board)
  104. fastboot_okay(board, response);
  105. else
  106. fastboot_fail("Board not set", response);
  107. }
  108. static void getvar_current_slot(char *var_parameter, char *response)
  109. {
  110. /* A/B not implemented, for now always return _a */
  111. fastboot_okay("_a", response);
  112. }
  113. static void getvar_slot_suffixes(char *var_parameter, char *response)
  114. {
  115. fastboot_okay("_a,_b", response);
  116. }
  117. static void getvar_has_slot(char *part_name, char *response)
  118. {
  119. if (part_name && (!strcmp(part_name, "boot") ||
  120. !strcmp(part_name, "system")))
  121. fastboot_okay("yes", response);
  122. else
  123. fastboot_okay("no", response);
  124. }
  125. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  126. static void getvar_partition_type(char *part_name, char *response)
  127. {
  128. int r;
  129. struct blk_desc *dev_desc;
  130. disk_partition_t part_info;
  131. r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
  132. response);
  133. if (r >= 0) {
  134. r = fs_set_blk_dev_with_part(dev_desc, r);
  135. if (r < 0)
  136. fastboot_fail("failed to set partition", response);
  137. else
  138. fastboot_okay(fs_get_type_name(), response);
  139. }
  140. }
  141. #endif
  142. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  143. static void getvar_partition_size(char *part_name, char *response)
  144. {
  145. int r;
  146. size_t size;
  147. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  148. struct blk_desc *dev_desc;
  149. disk_partition_t part_info;
  150. r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
  151. response);
  152. if (r >= 0)
  153. size = part_info.size;
  154. #endif
  155. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
  156. struct part_info *part_info;
  157. r = fastboot_nand_get_part_info(part_name, &part_info, response);
  158. if (r >= 0)
  159. size = part_info->size;
  160. #endif
  161. if (r >= 0)
  162. fastboot_response("OKAY", response, "0x%016zx", size);
  163. }
  164. #endif
  165. /**
  166. * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
  167. *
  168. * @cmd_parameter: Pointer to command parameter
  169. * @response: Pointer to fastboot response buffer
  170. *
  171. * Look up cmd_parameter first as an environment variable of the form
  172. * fastboot.<cmd_parameter>, if that exists return use its value to set
  173. * response.
  174. *
  175. * Otherwise lookup the name of variable and execute the appropriate
  176. * function to return the requested value.
  177. */
  178. void fastboot_getvar(char *cmd_parameter, char *response)
  179. {
  180. if (!cmd_parameter) {
  181. fastboot_fail("missing var", response);
  182. } else {
  183. #define FASTBOOT_ENV_PREFIX "fastboot."
  184. int i;
  185. char *var_parameter = cmd_parameter;
  186. char envstr[FASTBOOT_RESPONSE_LEN];
  187. const char *s;
  188. snprintf(envstr, sizeof(envstr) - 1,
  189. FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
  190. s = env_get(envstr);
  191. if (s) {
  192. fastboot_response("OKAY", response, "%s", s);
  193. return;
  194. }
  195. strsep(&var_parameter, ":");
  196. for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
  197. if (!strcmp(getvar_dispatch[i].variable,
  198. cmd_parameter)) {
  199. getvar_dispatch[i].dispatch(var_parameter,
  200. response);
  201. return;
  202. }
  203. }
  204. pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
  205. fastboot_fail("Variable not implemented", response);
  206. }
  207. }