xref: /rk3399_rockchip-uboot/common/fb_mmc.c (revision d622ac39274a949b6445f1bfd92dc1644014388b)
1 /*
2  * Copyright 2014 Broadcom Corporation.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <config.h>
8 #include <common.h>
9 #include <fb_mmc.h>
10 #include <part.h>
11 #include <aboot.h>
12 #include <sparse_format.h>
13 
14 #ifndef CONFIG_FASTBOOT_GPT_NAME
15 #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
16 #endif
17 
18 /* The 64 defined bytes plus the '\0' */
19 #define RESPONSE_LEN	(64 + 1)
20 
21 static char *response_str;
22 
23 void fastboot_fail(const char *s)
24 {
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);
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);
48 		fastboot_fail("too large for partition");
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);
58 		fastboot_fail("failed writing to device");
59 		return;
60 	}
61 
62 	printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
63 	       part_name);
64 	fastboot_okay("");
65 }
66 
67 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
68 			unsigned int download_bytes, char *response)
69 {
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");
79 		fastboot_fail("invalid mmc device");
80 		return;
81 	}
82 
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)) {
101 		error("cannot find partition: '%s'\n", cmd);
102 		fastboot_fail("cannot find partition");
103 		return;
104 	}
105 
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);
112 }
113