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