fb_mmc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef CONFIG_FASTBOOT_GPT_NAME
  13. #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
  14. #endif
  15. /* The 64 defined bytes plus the '\0' */
  16. #define RESPONSE_LEN (64 + 1)
  17. static char *response_str;
  18. void fastboot_fail(const char *s)
  19. {
  20. strncpy(response_str, "FAIL", 4);
  21. strncat(response_str, s, RESPONSE_LEN - 4 - 1);
  22. }
  23. void fastboot_okay(const char *s)
  24. {
  25. strncpy(response_str, "OKAY", 4);
  26. strncat(response_str, s, RESPONSE_LEN - 4 - 1);
  27. }
  28. static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
  29. const char *part_name, void *buffer,
  30. unsigned int download_bytes)
  31. {
  32. lbaint_t blkcnt;
  33. lbaint_t blks;
  34. /* determine number of blocks to write */
  35. blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
  36. blkcnt = blkcnt / info->blksz;
  37. if (blkcnt > info->size) {
  38. error("too large for partition: '%s'\n", part_name);
  39. fastboot_fail("too large for partition");
  40. return;
  41. }
  42. puts("Flashing Raw Image\n");
  43. blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
  44. buffer);
  45. if (blks != blkcnt) {
  46. error("failed writing to device %d\n", dev_desc->dev);
  47. fastboot_fail("failed writing to device");
  48. return;
  49. }
  50. printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
  51. part_name);
  52. fastboot_okay("");
  53. }
  54. void fb_mmc_flash_write(const char *cmd, void *download_buffer,
  55. unsigned int download_bytes, char *response)
  56. {
  57. block_dev_desc_t *dev_desc;
  58. disk_partition_t info;
  59. /* initialize the response buffer */
  60. response_str = response;
  61. dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  62. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  63. error("invalid mmc device\n");
  64. fastboot_fail("invalid mmc device");
  65. return;
  66. }
  67. if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
  68. printf("%s: updating MBR, Primary and Backup GPT(s)\n",
  69. __func__);
  70. if (is_valid_gpt_buf(dev_desc, download_buffer)) {
  71. printf("%s: invalid GPT - refusing to write to flash\n",
  72. __func__);
  73. fastboot_fail("invalid GPT partition");
  74. return;
  75. }
  76. if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
  77. printf("%s: writing GPT partitions failed\n", __func__);
  78. fastboot_fail("writing GPT partitions failed");
  79. return;
  80. }
  81. printf("........ success\n");
  82. fastboot_okay("");
  83. return;
  84. } else if (get_partition_info_efi_by_name(dev_desc, cmd, &info)) {
  85. error("cannot find partition: '%s'\n", cmd);
  86. fastboot_fail("cannot find partition");
  87. return;
  88. }
  89. if (is_sparse_image(download_buffer))
  90. write_sparse_image(dev_desc, &info, cmd, download_buffer,
  91. download_bytes);
  92. else
  93. write_raw_image(dev_desc, &info, cmd, download_buffer,
  94. download_bytes);
  95. }