fb_mmc.c 4.9 KB

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