]>
Commit | Line | Data |
---|---|---|
c0aebb33 SR |
1 | /* |
2 | * Copyright 2014 Broadcom Corporation. | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
0ff7e585 | 7 | #include <config.h> |
c0aebb33 SR |
8 | #include <common.h> |
9 | #include <fb_mmc.h> | |
10 | #include <part.h> | |
e5bf9878 SR |
11 | #include <aboot.h> |
12 | #include <sparse_format.h> | |
c0aebb33 | 13 | |
0ff7e585 SR |
14 | #ifndef CONFIG_FASTBOOT_GPT_NAME |
15 | #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME | |
16 | #endif | |
17 | ||
c0aebb33 SR |
18 | /* The 64 defined bytes plus the '\0' */ |
19 | #define RESPONSE_LEN (64 + 1) | |
20 | ||
21 | static char *response_str; | |
22 | ||
e5bf9878 | 23 | void fastboot_fail(const char *s) |
c0aebb33 | 24 | { |
e5bf9878 SR |
25 | strncpy(response_str, "FAIL", 4); |
26 | strncat(response_str, s, RESPONSE_LEN - 4 - 1); | |
27 | } | |
28 | ||
29 | void fastboot_okay(const char *s) | |
30 | { | |
31 | strncpy(response_str, "OKAY", 4); | |
32 | strncat(response_str, s, RESPONSE_LEN - 4 - 1); | |
c0aebb33 SR |
33 | } |
34 | ||
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) | |
38 | { | |
39 | lbaint_t blkcnt; | |
40 | lbaint_t blks; | |
41 | ||
42 | /* determine number of blocks to write */ | |
43 | blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); | |
44 | blkcnt = blkcnt / info->blksz; | |
45 | ||
46 | if (blkcnt > info->size) { | |
47 | error("too large for partition: '%s'\n", part_name); | |
e5bf9878 | 48 | fastboot_fail("too large for partition"); |
c0aebb33 SR |
49 | return; |
50 | } | |
51 | ||
52 | puts("Flashing Raw Image\n"); | |
53 | ||
54 | blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt, | |
55 | buffer); | |
56 | if (blks != blkcnt) { | |
57 | error("failed writing to device %d\n", dev_desc->dev); | |
e5bf9878 | 58 | fastboot_fail("failed writing to device"); |
c0aebb33 SR |
59 | return; |
60 | } | |
61 | ||
62 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, | |
63 | part_name); | |
e5bf9878 | 64 | fastboot_okay(""); |
c0aebb33 SR |
65 | } |
66 | ||
67 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, | |
68 | unsigned int download_bytes, char *response) | |
69 | { | |
c0aebb33 SR |
70 | block_dev_desc_t *dev_desc; |
71 | disk_partition_t info; | |
72 | ||
73 | /* initialize the response buffer */ | |
74 | response_str = response; | |
75 | ||
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"); | |
e5bf9878 | 79 | fastboot_fail("invalid mmc device"); |
c0aebb33 SR |
80 | return; |
81 | } | |
82 | ||
0ff7e585 SR |
83 | if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { |
84 | printf("%s: updating MBR, Primary and Backup GPT(s)\n", | |
85 | __func__); | |
86 | if (is_valid_gpt_buf(dev_desc, download_buffer)) { | |
87 | printf("%s: invalid GPT - refusing to write to flash\n", | |
88 | __func__); | |
89 | fastboot_fail("invalid GPT partition"); | |
90 | return; | |
91 | } | |
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"); | |
95 | return; | |
96 | } | |
97 | printf("........ success\n"); | |
98 | fastboot_okay(""); | |
99 | return; | |
100 | } else if (get_partition_info_efi_by_name(dev_desc, cmd, &info)) { | |
c0aebb33 | 101 | error("cannot find partition: '%s'\n", cmd); |
e5bf9878 | 102 | fastboot_fail("cannot find partition"); |
c0aebb33 SR |
103 | return; |
104 | } | |
105 | ||
e5bf9878 SR |
106 | if (is_sparse_image(download_buffer)) |
107 | write_sparse_image(dev_desc, &info, cmd, download_buffer, | |
108 | download_bytes); | |
109 | else | |
110 | write_raw_image(dev_desc, &info, cmd, download_buffer, | |
111 | download_bytes); | |
c0aebb33 | 112 | } |