xref: /rk3399_rockchip-uboot/common/fb_mmc.c (revision c0aebb3382133c99bc458d96b562703dae4ae928)
1 /*
2  * Copyright 2014 Broadcom Corporation.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <fb_mmc.h>
9 #include <part.h>
10 
11 /* The 64 defined bytes plus the '\0' */
12 #define RESPONSE_LEN	(64 + 1)
13 
14 static char *response_str;
15 
16 static void fastboot_resp(const char *s)
17 {
18 	strncpy(response_str, s, RESPONSE_LEN);
19 	response_str[RESPONSE_LEN - 1] = '\0';
20 }
21 
22 static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
23 		const char *part_name, void *buffer,
24 		unsigned int download_bytes)
25 {
26 	lbaint_t blkcnt;
27 	lbaint_t blks;
28 
29 	/* determine number of blocks to write */
30 	blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
31 	blkcnt = blkcnt / info->blksz;
32 
33 	if (blkcnt > info->size) {
34 		error("too large for partition: '%s'\n", part_name);
35 		fastboot_resp("FAILtoo large for partition");
36 		return;
37 	}
38 
39 	puts("Flashing Raw Image\n");
40 
41 	blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
42 				     buffer);
43 	if (blks != blkcnt) {
44 		error("failed writing to device %d\n", dev_desc->dev);
45 		fastboot_resp("FAILfailed writing to device");
46 		return;
47 	}
48 
49 	printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
50 	       part_name);
51 	fastboot_resp("OKAY");
52 }
53 
54 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
55 			unsigned int download_bytes, char *response)
56 {
57 	int ret;
58 	block_dev_desc_t *dev_desc;
59 	disk_partition_t info;
60 
61 	/* initialize the response buffer */
62 	response_str = response;
63 
64 	dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
65 	if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
66 		error("invalid mmc device\n");
67 		fastboot_resp("FAILinvalid mmc device");
68 		return;
69 	}
70 
71 	ret = get_partition_info_efi_by_name(dev_desc, cmd, &info);
72 	if (ret) {
73 		error("cannot find partition: '%s'\n", cmd);
74 		fastboot_resp("FAILcannot find partition");
75 		return;
76 	}
77 
78 	write_raw_image(dev_desc, &info, cmd, download_buffer,
79 			download_bytes);
80 }
81