2 * Copyright 2014 Broadcom Corporation.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <sparse_format.h>
14 #ifndef CONFIG_FASTBOOT_GPT_NAME
15 #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
18 /* The 64 defined bytes plus the '\0' */
19 #define RESPONSE_LEN (64 + 1)
21 static char *response_str;
23 void fastboot_fail(const char *s)
25 strncpy(response_str, "FAIL", 4);
26 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
29 void fastboot_okay(const char *s)
31 strncpy(response_str, "OKAY", 4);
32 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
35 static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
36 const char *part_name, void *buffer,
37 unsigned int download_bytes)
42 /* determine number of blocks to write */
43 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
44 blkcnt = blkcnt / info->blksz;
46 if (blkcnt > info->size) {
47 error("too large for partition: '%s'\n", part_name);
48 fastboot_fail("too large for partition");
52 puts("Flashing Raw Image\n");
54 blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
57 error("failed writing to device %d\n", dev_desc->dev);
58 fastboot_fail("failed writing to device");
62 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
67 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
68 unsigned int download_bytes, char *response)
70 block_dev_desc_t *dev_desc;
71 disk_partition_t info;
73 /* initialize the response buffer */
74 response_str = response;
76 dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
77 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
78 error("invalid mmc device\n");
79 fastboot_fail("invalid mmc device");
83 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
84 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
86 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
87 printf("%s: invalid GPT - refusing to write to flash\n",
89 fastboot_fail("invalid GPT partition");
92 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
93 printf("%s: writing GPT partitions failed\n", __func__);
94 fastboot_fail("writing GPT partitions failed");
97 printf("........ success\n");
100 } else if (get_partition_info_efi_by_name(dev_desc, cmd, &info)) {
101 error("cannot find partition: '%s'\n", cmd);
102 fastboot_fail("cannot find partition");
106 if (is_sparse_image(download_buffer))
107 write_sparse_image(dev_desc, &info, cmd, download_buffer,
110 write_raw_image(dev_desc, &info, cmd, download_buffer,