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