fb_mmc.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /* The 64 defined bytes plus the '\0' */
  10. #define RESPONSE_LEN (64 + 1)
  11. static char *response_str;
  12. static void fastboot_resp(const char *s)
  13. {
  14. strncpy(response_str, s, RESPONSE_LEN);
  15. response_str[RESPONSE_LEN - 1] = '\0';
  16. }
  17. static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
  18. const char *part_name, void *buffer,
  19. unsigned int download_bytes)
  20. {
  21. lbaint_t blkcnt;
  22. lbaint_t blks;
  23. /* determine number of blocks to write */
  24. blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
  25. blkcnt = blkcnt / info->blksz;
  26. if (blkcnt > info->size) {
  27. error("too large for partition: '%s'\n", part_name);
  28. fastboot_resp("FAILtoo large for partition");
  29. return;
  30. }
  31. puts("Flashing Raw Image\n");
  32. blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
  33. buffer);
  34. if (blks != blkcnt) {
  35. error("failed writing to device %d\n", dev_desc->dev);
  36. fastboot_resp("FAILfailed writing to device");
  37. return;
  38. }
  39. printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
  40. part_name);
  41. fastboot_resp("OKAY");
  42. }
  43. void fb_mmc_flash_write(const char *cmd, void *download_buffer,
  44. unsigned int download_bytes, char *response)
  45. {
  46. int ret;
  47. block_dev_desc_t *dev_desc;
  48. disk_partition_t info;
  49. /* initialize the response buffer */
  50. response_str = response;
  51. dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  52. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  53. error("invalid mmc device\n");
  54. fastboot_resp("FAILinvalid mmc device");
  55. return;
  56. }
  57. ret = get_partition_info_efi_by_name(dev_desc, cmd, &info);
  58. if (ret) {
  59. error("cannot find partition: '%s'\n", cmd);
  60. fastboot_resp("FAILcannot find partition");
  61. return;
  62. }
  63. write_raw_image(dev_desc, &info, cmd, download_buffer,
  64. download_bytes);
  65. }