hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * (C) Copyright 2017 rkparm Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
 
#include <common.h>
#include <malloc.h>
#ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
#include <asm/arch/rk_atags.h>
#endif
 
#ifdef HAVE_BLOCK_DEVICE
#define RK_PARAM_OFFSET            0x2000
#define MAX_PARAM_SIZE            (1024 * 64)
 
struct rkparm_param {
   u32 tag;
   u32 length;
   char params[1];
   u32 crc;
};
 
struct rkparm_part {
   char name[PART_NAME_LEN];
   unsigned long start;
   unsigned long size;
   struct list_head node;
};
 
 
static LIST_HEAD(parts_head);
 
/*
 * What's this?
 *
 * There maybe two different storage media need to use this partition driver,
 * e.g. rkand with SD card. So we need a flag info to recognize it.
 */
static int dev_num = -1;
 
static int rkparm_param_parse(char *param, struct list_head *parts_head,
                 struct blk_desc *dev_desc)
{
   struct rkparm_part *part;
   const char *cmdline = strstr(param, "CMDLINE:");
   const char *blkdev_parts;
   char *cmdline_end, *next, *pend;
   int len, offset = 0;
   unsigned long size, start;
 
   if (!cmdline) {
       debug("RKPARM: Invalid parameter part table from storage\n");
       return -EINVAL;
   }
 
   blkdev_parts = strstr(cmdline, "mtdparts");
   next = strchr(blkdev_parts, ':');
   cmdline_end = strstr(cmdline, "\n"); /* end by '\n' */
   *cmdline_end = '\0';
   /*
    * 1. skip "CMDLINE:"
    * 2. Initrd fixup: remove unused "initrd=0x...,0x...", this for
    *    compatible with legacy parameter.txt
    */
   env_update_filter("bootargs", cmdline + strlen("CMDLINE:"), "initrd=");
 
   INIT_LIST_HEAD(parts_head);
   while (next) {
       /* Skip ':' and ',' */
       next++;
       if (*next == '-') {
           size = (~0UL);
           next++;
       } else {
           size = simple_strtoul(next, &next, 16);
       }
       /* Skip '@' */
       next++;
       start = simple_strtoul(next, &next, 16);
       next++;
       pend =  strchr(next, ')');
       if (!pend)
           break;
       len = min_t(int, pend - next, PART_NAME_LEN);
       part = malloc(sizeof(*part));
       if (!part) {
           printf("out of memory\n");
           break;
       }
#ifndef PARTS_RKPARM
       if (dev_desc->if_type != IF_TYPE_RKNAND)
           offset = RK_PARAM_OFFSET;
#endif
       part->start = start + offset;
       /* Last partition use all remain space */
       if (size == (~0UL))
           size = dev_desc->lba - part->start;
       part->size = size;
       strncpy(part->name, next, len);
       part->name[len] = '\0';
       list_add_tail(&part->node, parts_head);
       next = strchr(next, ',');
   }
 
   dev_num = ((dev_desc->if_type << 8) + dev_desc->devnum);
 
   return 0;
}
 
static int rkparm_init_param(struct blk_desc *dev_desc,
                     struct list_head *parts_head)
{
   char *parts_list;
 
   /*
    * There are ways to get partition tables:
    *
    * 1. macro 'PARTS_RKPARM' string list (No RK_PARAM_OFFSET for every partition),
    * 2. Legacy rk parameter in flash @RK_PARAM_OFFSET sectors offset
    */
#ifdef PARTS_RKPARM
   parts_list = PARTS_RKPARM;
#else
   struct rkparm_param *param;
   int offset = 0;
   int ret;
 
   param = memalign(ARCH_DMA_MINALIGN, MAX_PARAM_SIZE);
   if (!param) {
       printf("out of memory\n");
       return -ENOMEM;
   }
 
   if (dev_desc->if_type != IF_TYPE_RKNAND)
       offset = RK_PARAM_OFFSET;
 
   ret = blk_dread(dev_desc, offset, MAX_PARAM_SIZE >> 9, (ulong *)param);
   if (ret != (MAX_PARAM_SIZE >> 9)) {
       printf("%s param read fail\n", __func__);
       return -EINVAL;
   }
   parts_list = param->params;
#endif
   return rkparm_param_parse(parts_list, parts_head, dev_desc);
}
 
static void part_print_rkparm(struct blk_desc *dev_desc)
{
   int ret = 0;
   struct list_head *node;
   struct rkparm_part *p = NULL;
   int i = 0;
 
   if (list_empty(&parts_head) ||
       (dev_num != ((dev_desc->if_type << 8) + dev_desc->devnum)))
       ret = rkparm_init_param(dev_desc, &parts_head);
 
   if (ret) {
       printf("%s Invalid rkparm parameter\n", __func__);
       return;
   }
 
   printf("Part\tStart LBA\tSize\t\tName\n");
   list_for_each(node, &parts_head) {
       p = list_entry(node, struct rkparm_part, node);
       printf("%3d\t0x%08lx\t0x%08lx\t%s\n", (i++ + 1),
              p->start, p->size, p->name);
   }
 
   return;
}
 
static int part_get_info_rkparm(struct blk_desc *dev_desc, int idx,
             disk_partition_t *info)
{
   struct list_head *node;
   struct rkparm_part *p = NULL;
   int part_num = 0;
   int ret = 0;
 
   if (idx < 1) {
       printf("%s Invalid partition no.%d\n", __func__, idx);
       return -EINVAL;
   }
 
   if (list_empty(&parts_head) ||
       (dev_num != ((dev_desc->if_type << 8) + dev_desc->devnum))) {
       ret = rkparm_init_param(dev_desc, &parts_head);
       if (ret) {
           printf("%s Invalid rkparm partition\n", __func__);
           return -1;
       }
   }
 
   list_for_each(node, &parts_head) {
       p = list_entry(node, struct rkparm_part, node);
       part_num ++;
       if (idx == part_num)
           break;
   }
 
   if (part_num < idx) {
       debug("%s Invalid partition no.%d\n", __func__, idx);
       return -EINVAL;
   }
 
   info->start = p->start;
   info->size = p->size;
   info->blksz = dev_desc->blksz;
 
   sprintf((char *)info->name, "%s", p->name);
   strcpy((char *)info->type, "U-Boot");
   info->bootable = 0;
 
   return 0;
}
 
static int part_test_rkparm(struct blk_desc *dev_desc)
{
   int ret = 0;
 
   if (list_empty(&parts_head) ||
       (dev_num != ((dev_desc->if_type << 8) + dev_desc->devnum)))
       ret = rkparm_init_param(dev_desc, &parts_head);
   if (ret)
       ret = -1;
 
   return ret;
}
/*
 * Add an 'b_' prefix so it comes before 'dos' and after 'a_efi' in the linker
 * list. We need to check EFI first, and then rkparm partition
 */
U_BOOT_PART_TYPE(b_rkparm) = {
#ifdef PARTS_RKPARM
   .name        = "RKPARM(FIXED)",
#else
   .name        = "RKPARM",
#endif
   .part_type    = PART_TYPE_RKPARM,
   .max_entries    = RKPARM_ENTRY_NUMBERS,
   .get_info    = part_get_info_ptr(part_get_info_rkparm),
   .print        = part_print_ptr(part_print_rkparm),
   .test        = part_test_rkparm,
};
#endif