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 int get_partition_info_efi_by_name_or_alias(block_dev_desc_t
*dev_desc
,
37 const char *name
, disk_partition_t
*info
)
41 ret
= get_partition_info_efi_by_name(dev_desc
, name
, info
);
43 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
44 char env_alias_name
[25 + 32 + 1];
45 char *aliased_part_name
;
48 strcpy(env_alias_name
, "fastboot_partition_alias_");
49 strncat(env_alias_name
, name
, 32);
50 aliased_part_name
= getenv(env_alias_name
);
51 if (aliased_part_name
!= NULL
)
52 ret
= get_partition_info_efi_by_name(dev_desc
,
53 aliased_part_name
, info
);
58 static void write_raw_image(block_dev_desc_t
*dev_desc
, disk_partition_t
*info
,
59 const char *part_name
, void *buffer
,
60 unsigned int download_bytes
)
65 /* determine number of blocks to write */
66 blkcnt
= ((download_bytes
+ (info
->blksz
- 1)) & ~(info
->blksz
- 1));
67 blkcnt
= blkcnt
/ info
->blksz
;
69 if (blkcnt
> info
->size
) {
70 error("too large for partition: '%s'\n", part_name
);
71 fastboot_fail("too large for partition");
75 puts("Flashing Raw Image\n");
77 blks
= dev_desc
->block_write(dev_desc
->dev
, info
->start
, blkcnt
,
80 error("failed writing to device %d\n", dev_desc
->dev
);
81 fastboot_fail("failed writing to device");
85 printf("........ wrote " LBAFU
" bytes to '%s'\n", blkcnt
* info
->blksz
,
90 void fb_mmc_flash_write(const char *cmd
, void *download_buffer
,
91 unsigned int download_bytes
, char *response
)
93 block_dev_desc_t
*dev_desc
;
94 disk_partition_t info
;
96 /* initialize the response buffer */
97 response_str
= response
;
99 dev_desc
= get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV
);
100 if (!dev_desc
|| dev_desc
->type
== DEV_TYPE_UNKNOWN
) {
101 error("invalid mmc device\n");
102 fastboot_fail("invalid mmc device");
106 if (strcmp(cmd
, CONFIG_FASTBOOT_GPT_NAME
) == 0) {
107 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
109 if (is_valid_gpt_buf(dev_desc
, download_buffer
)) {
110 printf("%s: invalid GPT - refusing to write to flash\n",
112 fastboot_fail("invalid GPT partition");
115 if (write_mbr_and_gpt_partitions(dev_desc
, download_buffer
)) {
116 printf("%s: writing GPT partitions failed\n", __func__
);
117 fastboot_fail("writing GPT partitions failed");
120 printf("........ success\n");
123 } else if (get_partition_info_efi_by_name_or_alias(dev_desc
, cmd
, &info
)) {
124 error("cannot find partition: '%s'\n", cmd
);
125 fastboot_fail("cannot find partition");
129 if (is_sparse_image(download_buffer
))
130 write_sparse_image(dev_desc
, &info
, cmd
, download_buffer
,
133 write_raw_image(dev_desc
, &info
, cmd
, download_buffer
,
137 void fb_mmc_erase(const char *cmd
, char *response
)
140 block_dev_desc_t
*dev_desc
;
141 disk_partition_t info
;
142 lbaint_t blks
, blks_start
, blks_size
, grp_size
;
143 struct mmc
*mmc
= find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV
);
146 error("invalid mmc device");
147 fastboot_fail("invalid mmc device");
151 /* initialize the response buffer */
152 response_str
= response
;
154 dev_desc
= get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV
);
155 if (!dev_desc
|| dev_desc
->type
== DEV_TYPE_UNKNOWN
) {
156 error("invalid mmc device");
157 fastboot_fail("invalid mmc device");
161 ret
= get_partition_info_efi_by_name_or_alias(dev_desc
, cmd
, &info
);
163 error("cannot find partition: '%s'", cmd
);
164 fastboot_fail("cannot find partition");
168 /* Align blocks to erase group size to avoid erasing other partitions */
169 grp_size
= mmc
->erase_grp_size
;
170 blks_start
= (info
.start
+ grp_size
- 1) & ~(grp_size
- 1);
171 if (info
.size
>= grp_size
)
172 blks_size
= (info
.size
- (blks_start
- info
.start
)) &
177 printf("Erasing blocks " LBAFU
" to " LBAFU
" due to alignment\n",
178 blks_start
, blks_start
+ blks_size
);
180 blks
= dev_desc
->block_erase(dev_desc
->dev
, blks_start
, blks_size
);
181 if (blks
!= blks_size
) {
182 error("failed erasing from device %d", dev_desc
->dev
);
183 fastboot_fail("failed erasing from device");
187 printf("........ erased " LBAFU
" bytes from '%s'\n",
188 blks_size
* info
.blksz
, cmd
);