2 * Copyright 2014 Broadcom Corporation.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <sparse_format.h>
15 #ifndef CONFIG_FASTBOOT_GPT_NAME
16 #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
19 /* The 64 defined bytes plus the '\0' */
20 #define RESPONSE_LEN (64 + 1)
22 static char *response_str;
24 void fastboot_fail(const char *s)
26 strncpy(response_str, "FAIL\0", 5);
27 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
30 void fastboot_okay(const char *s)
32 strncpy(response_str, "OKAY\0", 5);
33 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
36 static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
37 const char *part_name, void *buffer,
38 unsigned int download_bytes)
43 /* determine number of blocks to write */
44 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
45 blkcnt = blkcnt / info->blksz;
47 if (blkcnt > info->size) {
48 error("too large for partition: '%s'\n", part_name);
49 fastboot_fail("too large for partition");
53 puts("Flashing Raw Image\n");
55 blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
58 error("failed writing to device %d\n", dev_desc->dev);
59 fastboot_fail("failed writing to device");
63 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
68 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
69 unsigned int download_bytes, char *response)
71 block_dev_desc_t *dev_desc;
72 disk_partition_t info;
74 /* initialize the response buffer */
75 response_str = response;
77 dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
78 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
79 error("invalid mmc device\n");
80 fastboot_fail("invalid mmc device");
84 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
85 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
87 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
88 printf("%s: invalid GPT - refusing to write to flash\n",
90 fastboot_fail("invalid GPT partition");
93 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
94 printf("%s: writing GPT partitions failed\n", __func__);
95 fastboot_fail("writing GPT partitions failed");
98 printf("........ success\n");
101 } else if (get_partition_info_efi_by_name(dev_desc, cmd, &info)) {
102 error("cannot find partition: '%s'\n", cmd);
103 fastboot_fail("cannot find partition");
107 if (is_sparse_image(download_buffer))
108 write_sparse_image(dev_desc, &info, cmd, download_buffer,
111 write_raw_image(dev_desc, &info, cmd, download_buffer,
115 void fb_mmc_erase(const char *cmd, char *response)
118 block_dev_desc_t *dev_desc;
119 disk_partition_t info;
120 lbaint_t blks, blks_start, blks_size, grp_size;
121 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
124 error("invalid mmc device");
125 fastboot_fail("invalid mmc device");
129 /* initialize the response buffer */
130 response_str = response;
132 dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
133 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
134 error("invalid mmc device");
135 fastboot_fail("invalid mmc device");
139 ret = get_partition_info_efi_by_name(dev_desc, cmd, &info);
141 error("cannot find partition: '%s'", cmd);
142 fastboot_fail("cannot find partition");
146 /* Align blocks to erase group size to avoid erasing other partitions */
147 grp_size = mmc->erase_grp_size;
148 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
149 if (info.size >= grp_size)
150 blks_size = (info.size - (blks_start - info.start)) &
155 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
156 blks_start, blks_start + blks_size);
158 blks = dev_desc->block_erase(dev_desc->dev, blks_start, blks_size);
159 if (blks != blks_size) {
160 error("failed erasing from device %d", dev_desc->dev);
161 fastboot_fail("failed erasing from device");
165 printf("........ erased " LBAFU " bytes from '%s'\n",
166 blks_size * info.blksz, cmd);