xref: /rk3399_rockchip-uboot/disk/part_rkparm.c (revision d14f7e1ef9967e7b1e8b70125c0d2eb27fe5fc06)
1 /*
2  * (C) Copyright 2017 rkparm Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <malloc.h>
9 
10 #ifdef HAVE_BLOCK_DEVICE
11 #define RK_PARAM_OFFSET			0x2000
12 #define MAX_PARAM_SIZE			(1024 * 64)
13 
14 struct rkparm_param {
15 	u32 tag;
16 	u32 length;
17 	char params[1];
18 	u32 crc;
19 };
20 
21 struct rkparm_part {
22 	char name[PART_NAME_LEN];
23 	unsigned long start;
24 	unsigned long size;
25 	struct list_head node;
26 };
27 
28 
29 static LIST_HEAD(parts_head);
30 
31 static int rkparm_param_parse(char *param, struct list_head *parts_head,
32 			      struct blk_desc *dev_desc)
33 {
34 	struct rkparm_part *part;
35 	const char *cmdline = strstr(param, "CMDLINE:");
36 	char *cmdline_end = strstr(cmdline, "\n"); /* end by '\n' */
37 	const char *blkdev_parts = strstr(cmdline, "mtdparts");
38 	const char *blkdev_def = strchr(blkdev_parts, ':') + 1;
39 	char *next = (char *)blkdev_def;
40 	char *pend;
41 	int len, offset = 0;
42 	unsigned long size, start;
43 
44 	if (!cmdline) {
45 		printf("invalid parameter\n");
46 		return -EINVAL;
47 	}
48 
49 	*cmdline_end = '\0';
50 	/* skip "CMDLINE:" */
51 	env_update("bootargs", cmdline + strlen("CMDLINE:"));
52 
53 	/*
54 	 * Initrd fixup: remove unused "initrd=0x...,0x...", this for
55 	 * compatible with legacy parameter.txt
56 	 */
57 	env_delete("bootargs", "initrd=");
58 
59 	while (*next) {
60 		if (*next == '-') {
61 			size = (~0UL);
62 			next++;
63 		} else {
64 			size = simple_strtoul(next, &next, 16);
65 		}
66 		next++;
67 		start = simple_strtoul(next, &next, 16);
68 		next++;
69 		pend =  strchr(next, ')');
70 		if (!pend)
71 			break;
72 		len = min_t(int, pend - next, PART_NAME_LEN);
73 		part = malloc(sizeof(*part));
74 		if (!part) {
75 			printf("out of memory\n");
76 			break;
77 		}
78 		if (dev_desc->if_type != IF_TYPE_RKNAND)
79 			offset = RK_PARAM_OFFSET;
80 		part->start = start + offset;
81 		part->size = size;
82 		strncpy(part->name, next, len);
83 		part->name[len] = '\0';
84 		next = strchr(next, ',');
85 		next++;
86 		list_add_tail(&part->node, parts_head);
87 	}
88 
89 	return 0;
90 }
91 
92 static int rkparm_init_param(struct blk_desc *dev_desc,
93 				struct list_head *parts_head)
94 {
95 	struct rkparm_param *param;
96 	int offset = 0;
97 	int ret;
98 
99 	param = memalign(ARCH_DMA_MINALIGN, MAX_PARAM_SIZE);
100 	if (!param) {
101 		printf("out of memory\n");
102 		return -ENOMEM;
103 	}
104 
105 	if (dev_desc->if_type != IF_TYPE_RKNAND)
106 		offset = RK_PARAM_OFFSET;
107 
108 	ret = blk_dread(dev_desc, offset, MAX_PARAM_SIZE >> 9, (ulong *)param);
109 	if (ret != (MAX_PARAM_SIZE >> 9)) {
110 		printf("%s param read fail\n", __func__);
111 		return -EINVAL;
112 	}
113 
114 	return rkparm_param_parse(param->params, parts_head, dev_desc);
115 
116 }
117 
118 static void part_print_rkparm(struct blk_desc *dev_desc)
119 {
120 	int ret = 0;
121 	struct list_head *node;
122 	struct rkparm_part *p = NULL;
123 	int i = 0;
124 
125 	if (list_empty(&parts_head))
126 		ret = rkparm_init_param(dev_desc, &parts_head);
127 
128 	if (ret) {
129 		printf("%s Invalid rkparm parameter\n", __func__);
130 		return;
131 	}
132 
133 	printf("Part\tStart LBA\tSize\t\tName\n");
134 	list_for_each(node, &parts_head) {
135 		p = list_entry(node, struct rkparm_part, node);
136 		printf("%3d\t0x%08lx\t0x%08lx\t%s\n", (i++ + 1),
137 		       p->start, p->size, p->name);
138 	}
139 
140 
141 	return;
142 }
143 
144 static int part_get_info_rkparm(struct blk_desc *dev_desc, int idx,
145 		      disk_partition_t *info)
146 {
147 	struct list_head *node;
148 	struct rkparm_part *p = NULL;
149 	int part_num = 1;
150 	int ret = 0;
151 
152 	if (idx < 1) {
153 		printf("%s Invalid partition no.%d\n", __func__, idx);
154 		return -EINVAL;
155 	}
156 
157 	if (list_empty(&parts_head))
158 		ret = rkparm_init_param(dev_desc, &parts_head);
159 
160 	if (ret) {
161 		printf("%s Invalid rkparm partition\n", __func__);
162 		return -1;
163 	}
164 
165 	list_for_each(node, &parts_head) {
166 		p = list_entry(node, struct rkparm_part, node);
167 		if (idx == part_num)
168 			break;
169 		part_num ++;
170 	}
171 
172 	if (part_num > idx) {
173 		printf("%s Invalid partition no.%d\n", __func__, idx);
174 		return -EINVAL;
175 	}
176 
177 	info->start = p->start;
178 	info->size = p->size << 9;
179 	info->blksz = dev_desc->blksz;
180 
181 	sprintf((char *)info->name, "%s", p->name);
182 	strcpy((char *)info->type, "U-Boot");
183 	info->bootable = 0;
184 
185 	return 0;
186 }
187 
188 static int part_test_rkparm(struct blk_desc *dev_desc)
189 {
190 	int ret = 0;
191 
192 	if (list_empty(&parts_head))
193 		ret = rkparm_init_param(dev_desc, &parts_head);
194 	if (ret)
195 		ret = -1;
196 
197 	return ret;
198 }
199 /*
200  * Add an 'b_' prefix so it comes before 'dos' and after 'a_efi' in the linker
201  * list. We need to check EFI first, and then rkparm partition
202  */
203 U_BOOT_PART_TYPE(b_rkparm) = {
204 	.name		= "RKPARM",
205 	.part_type	= PART_TYPE_RKPARM,
206 	.max_entries	= GPT_ENTRY_NUMBERS,
207 	.get_info	= part_get_info_ptr(part_get_info_rkparm),
208 	.print		= part_print_ptr(part_print_rkparm),
209 	.test		= part_test_rkparm,
210 };
211 #endif
212