fb_mmc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2014 Broadcom Corporation.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <config.h>
  7. #include <common.h>
  8. #include <fb_mmc.h>
  9. #include <part.h>
  10. #include <aboot.h>
  11. #include <sparse_format.h>
  12. #include <mmc.h>
  13. #ifndef CONFIG_FASTBOOT_GPT_NAME
  14. #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
  15. #endif
  16. /* The 64 defined bytes plus the '\0' */
  17. #define RESPONSE_LEN (64 + 1)
  18. static char *response_str;
  19. void fastboot_fail(const char *s)
  20. {
  21. strncpy(response_str, "FAIL\0", 5);
  22. strncat(response_str, s, RESPONSE_LEN - 4 - 1);
  23. }
  24. void fastboot_okay(const char *s)
  25. {
  26. strncpy(response_str, "OKAY\0", 5);
  27. strncat(response_str, s, RESPONSE_LEN - 4 - 1);
  28. }
  29. static int get_partition_info_efi_by_name_or_alias(block_dev_desc_t *dev_desc,
  30. const char *name, disk_partition_t *info)
  31. {
  32. int ret;
  33. ret = get_partition_info_efi_by_name(dev_desc, name, info);
  34. if (ret) {
  35. /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
  36. char env_alias_name[25 + 32 + 1];
  37. char *aliased_part_name;
  38. /* check for alias */
  39. strcpy(env_alias_name, "fastboot_partition_alias_");
  40. strncat(env_alias_name, name, 32);
  41. aliased_part_name = getenv(env_alias_name);
  42. if (aliased_part_name != NULL)
  43. ret = get_partition_info_efi_by_name(dev_desc,
  44. aliased_part_name, info);
  45. }
  46. return ret;
  47. }
  48. static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
  49. const char *part_name, void *buffer,
  50. unsigned int download_bytes)
  51. {
  52. lbaint_t blkcnt;
  53. lbaint_t blks;
  54. /* determine number of blocks to write */
  55. blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
  56. blkcnt = blkcnt / info->blksz;
  57. if (blkcnt > info->size) {
  58. error("too large for partition: '%s'\n", part_name);
  59. fastboot_fail("too large for partition");
  60. return;
  61. }
  62. puts("Flashing Raw Image\n");
  63. blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
  64. buffer);
  65. if (blks != blkcnt) {
  66. error("failed writing to device %d\n", dev_desc->dev);
  67. fastboot_fail("failed writing to device");
  68. return;
  69. }
  70. printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
  71. part_name);
  72. fastboot_okay("");
  73. }
  74. void fb_mmc_flash_write(const char *cmd, void *download_buffer,
  75. unsigned int download_bytes, char *response)
  76. {
  77. block_dev_desc_t *dev_desc;
  78. disk_partition_t info;
  79. /* initialize the response buffer */
  80. response_str = response;
  81. dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  82. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  83. error("invalid mmc device\n");
  84. fastboot_fail("invalid mmc device");
  85. return;
  86. }
  87. if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
  88. printf("%s: updating MBR, Primary and Backup GPT(s)\n",
  89. __func__);
  90. if (is_valid_gpt_buf(dev_desc, download_buffer)) {
  91. printf("%s: invalid GPT - refusing to write to flash\n",
  92. __func__);
  93. fastboot_fail("invalid GPT partition");
  94. return;
  95. }
  96. if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
  97. printf("%s: writing GPT partitions failed\n", __func__);
  98. fastboot_fail("writing GPT partitions failed");
  99. return;
  100. }
  101. printf("........ success\n");
  102. fastboot_okay("");
  103. return;
  104. } else if (get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
  105. error("cannot find partition: '%s'\n", cmd);
  106. fastboot_fail("cannot find partition");
  107. return;
  108. }
  109. if (is_sparse_image(download_buffer))
  110. write_sparse_image(dev_desc, &info, cmd, download_buffer,
  111. download_bytes);
  112. else
  113. write_raw_image(dev_desc, &info, cmd, download_buffer,
  114. download_bytes);
  115. }
  116. void fb_mmc_erase(const char *cmd, char *response)
  117. {
  118. int ret;
  119. block_dev_desc_t *dev_desc;
  120. disk_partition_t info;
  121. lbaint_t blks, blks_start, blks_size, grp_size;
  122. struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
  123. if (mmc == NULL) {
  124. error("invalid mmc device");
  125. fastboot_fail("invalid mmc device");
  126. return;
  127. }
  128. /* initialize the response buffer */
  129. response_str = response;
  130. dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  131. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  132. error("invalid mmc device");
  133. fastboot_fail("invalid mmc device");
  134. return;
  135. }
  136. ret = get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info);
  137. if (ret) {
  138. error("cannot find partition: '%s'", cmd);
  139. fastboot_fail("cannot find partition");
  140. return;
  141. }
  142. /* Align blocks to erase group size to avoid erasing other partitions */
  143. grp_size = mmc->erase_grp_size;
  144. blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
  145. if (info.size >= grp_size)
  146. blks_size = (info.size - (blks_start - info.start)) &
  147. (~(grp_size - 1));
  148. else
  149. blks_size = 0;
  150. printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
  151. blks_start, blks_start + blks_size);
  152. blks = dev_desc->block_erase(dev_desc->dev, blks_start, blks_size);
  153. if (blks != blks_size) {
  154. error("failed erasing from device %d", dev_desc->dev);
  155. fastboot_fail("failed erasing from device");
  156. return;
  157. }
  158. printf("........ erased " LBAFU " bytes from '%s'\n",
  159. blks_size * info.blksz, cmd);
  160. fastboot_okay("");
  161. }