blob: 0c48cf929f8fd568d4d06095ede231ab81a97a6a [file] [log] [blame]
Steve Raec0aebb32014-08-26 11:47:27 -07001/*
2 * Copyright 2014 Broadcom Corporation.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Steve Rae0ff7e582014-12-12 15:51:54 -08007#include <config.h>
Steve Raec0aebb32014-08-26 11:47:27 -07008#include <common.h>
9#include <fb_mmc.h>
10#include <part.h>
Steve Raee5bf9872014-08-26 11:47:30 -070011#include <aboot.h>
12#include <sparse_format.h>
Dileep Katta89792382015-02-17 18:48:23 +053013#include <mmc.h>
Steve Raec0aebb32014-08-26 11:47:27 -070014
Steve Rae0ff7e582014-12-12 15:51:54 -080015#ifndef CONFIG_FASTBOOT_GPT_NAME
16#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
17#endif
18
Steve Raec0aebb32014-08-26 11:47:27 -070019/* The 64 defined bytes plus the '\0' */
20#define RESPONSE_LEN (64 + 1)
21
22static char *response_str;
23
Steve Raee5bf9872014-08-26 11:47:30 -070024void fastboot_fail(const char *s)
Steve Raec0aebb32014-08-26 11:47:27 -070025{
Dileep Kattae8742072015-02-13 14:33:42 +080026 strncpy(response_str, "FAIL\0", 5);
Steve Raee5bf9872014-08-26 11:47:30 -070027 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
28}
29
30void fastboot_okay(const char *s)
31{
Dileep Kattae8742072015-02-13 14:33:42 +080032 strncpy(response_str, "OKAY\0", 5);
Steve Raee5bf9872014-08-26 11:47:30 -070033 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
Steve Raec0aebb32014-08-26 11:47:27 -070034}
35
Michael Scott8a418022015-03-11 10:02:31 -070036static int get_partition_info_efi_by_name_or_alias(block_dev_desc_t *dev_desc,
37 const char *name, disk_partition_t *info)
38{
39 int ret;
40
41 ret = get_partition_info_efi_by_name(dev_desc, name, info);
42 if (ret) {
43 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
44 char env_alias_name[25 + 32 + 1];
45 char *aliased_part_name;
46
47 /* check for alias */
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);
54 }
55 return ret;
56}
57
Steve Raec0aebb32014-08-26 11:47:27 -070058static 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)
61{
62 lbaint_t blkcnt;
63 lbaint_t blks;
64
65 /* determine number of blocks to write */
66 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
67 blkcnt = blkcnt / info->blksz;
68
69 if (blkcnt > info->size) {
70 error("too large for partition: '%s'\n", part_name);
Steve Raee5bf9872014-08-26 11:47:30 -070071 fastboot_fail("too large for partition");
Steve Raec0aebb32014-08-26 11:47:27 -070072 return;
73 }
74
75 puts("Flashing Raw Image\n");
76
77 blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
78 buffer);
79 if (blks != blkcnt) {
80 error("failed writing to device %d\n", dev_desc->dev);
Steve Raee5bf9872014-08-26 11:47:30 -070081 fastboot_fail("failed writing to device");
Steve Raec0aebb32014-08-26 11:47:27 -070082 return;
83 }
84
85 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
86 part_name);
Steve Raee5bf9872014-08-26 11:47:30 -070087 fastboot_okay("");
Steve Raec0aebb32014-08-26 11:47:27 -070088}
89
90void fb_mmc_flash_write(const char *cmd, void *download_buffer,
91 unsigned int download_bytes, char *response)
92{
Steve Raec0aebb32014-08-26 11:47:27 -070093 block_dev_desc_t *dev_desc;
94 disk_partition_t info;
95
96 /* initialize the response buffer */
97 response_str = response;
98
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");
Steve Raee5bf9872014-08-26 11:47:30 -0700102 fastboot_fail("invalid mmc device");
Steve Raec0aebb32014-08-26 11:47:27 -0700103 return;
104 }
105
Steve Rae0ff7e582014-12-12 15:51:54 -0800106 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
107 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
108 __func__);
109 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
110 printf("%s: invalid GPT - refusing to write to flash\n",
111 __func__);
112 fastboot_fail("invalid GPT partition");
113 return;
114 }
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");
118 return;
119 }
120 printf("........ success\n");
121 fastboot_okay("");
122 return;
Michael Scott8a418022015-03-11 10:02:31 -0700123 } else if (get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
Steve Raec0aebb32014-08-26 11:47:27 -0700124 error("cannot find partition: '%s'\n", cmd);
Steve Raee5bf9872014-08-26 11:47:30 -0700125 fastboot_fail("cannot find partition");
Steve Raec0aebb32014-08-26 11:47:27 -0700126 return;
127 }
128
Steve Raee5bf9872014-08-26 11:47:30 -0700129 if (is_sparse_image(download_buffer))
130 write_sparse_image(dev_desc, &info, cmd, download_buffer,
131 download_bytes);
132 else
133 write_raw_image(dev_desc, &info, cmd, download_buffer,
134 download_bytes);
Steve Raec0aebb32014-08-26 11:47:27 -0700135}
Dileep Katta89792382015-02-17 18:48:23 +0530136
137void fb_mmc_erase(const char *cmd, char *response)
138{
139 int ret;
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);
144
145 if (mmc == NULL) {
146 error("invalid mmc device");
147 fastboot_fail("invalid mmc device");
148 return;
149 }
150
151 /* initialize the response buffer */
152 response_str = response;
153
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");
158 return;
159 }
160
Michael Scott8a418022015-03-11 10:02:31 -0700161 ret = get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info);
Dileep Katta89792382015-02-17 18:48:23 +0530162 if (ret) {
163 error("cannot find partition: '%s'", cmd);
164 fastboot_fail("cannot find partition");
165 return;
166 }
167
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)) &
173 (~(grp_size - 1));
174 else
175 blks_size = 0;
176
177 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
178 blks_start, blks_start + blks_size);
179
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");
184 return;
185 }
186
187 printf("........ erased " LBAFU " bytes from '%s'\n",
188 blks_size * info.blksz, cmd);
189 fastboot_okay("");
190}