fb_mmc.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2014 Broadcom Corporation.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <fb_mmc.h>
  8. #include <part.h>
  9. #include <aboot.h>
  10. #include <sparse_format.h>
  11. /* The 64 defined bytes plus the '\0' */
  12. #define RESPONSE_LEN (64 + 1)
  13. static char *response_str;
  14. void fastboot_fail(const char *s)
  15. {
  16. strncpy(response_str, "FAIL", 4);
  17. strncat(response_str, s, RESPONSE_LEN - 4 - 1);
  18. }
  19. void fastboot_okay(const char *s)
  20. {
  21. strncpy(response_str, "OKAY", 4);
  22. strncat(response_str, s, RESPONSE_LEN - 4 - 1);
  23. }
  24. static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
  25. const char *part_name, void *buffer,
  26. unsigned int download_bytes)
  27. {
  28. lbaint_t blkcnt;
  29. lbaint_t blks;
  30. /* determine number of blocks to write */
  31. blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
  32. blkcnt = blkcnt / info->blksz;
  33. if (blkcnt > info->size) {
  34. error("too large for partition: '%s'\n", part_name);
  35. fastboot_fail("too large for partition");
  36. return;
  37. }
  38. puts("Flashing Raw Image\n");
  39. blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
  40. buffer);
  41. if (blks != blkcnt) {
  42. error("failed writing to device %d\n", dev_desc->dev);
  43. fastboot_fail("failed writing to device");
  44. return;
  45. }
  46. printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
  47. part_name);
  48. fastboot_okay("");
  49. }
  50. void fb_mmc_flash_write(const char *cmd, void *download_buffer,
  51. unsigned int download_bytes, char *response)
  52. {
  53. int ret;
  54. block_dev_desc_t *dev_desc;
  55. disk_partition_t info;
  56. /* initialize the response buffer */
  57. response_str = response;
  58. dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  59. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  60. error("invalid mmc device\n");
  61. fastboot_fail("invalid mmc device");
  62. return;
  63. }
  64. ret = get_partition_info_efi_by_name(dev_desc, cmd, &info);
  65. if (ret) {
  66. error("cannot find partition: '%s'\n", cmd);
  67. fastboot_fail("cannot find partition");
  68. return;
  69. }
  70. if (is_sparse_image(download_buffer))
  71. write_sparse_image(dev_desc, &info, cmd, download_buffer,
  72. download_bytes);
  73. else
  74. write_raw_image(dev_desc, &info, cmd, download_buffer,
  75. download_bytes);
  76. }