blob: 7ddb92448a32ac095da2e1731d25c7e671720cef [file] [log] [blame]
Lijuan Gao1259da72021-03-05 10:39:12 +08001/* Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#include "LocateDeviceTree.h"
Runmin Wange74d8042016-07-19 18:09:07 -070030#include "UpdateDeviceTree.h"
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053031#include <Library/Board.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070032#include <Library/BootLinux.h>
33#include <Library/PartitionTableUpdate.h>
34#include <Library/Rtic.h>
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080035// Variables to initialize board data
36
Jeevan Shriram17f173d2017-10-24 22:11:07 -070037STATIC int
38platform_dt_absolute_match (struct dt_entry *cur_dt_entry,
39 struct dt_entry_node *dt_list);
40STATIC struct dt_entry *
41platform_dt_match_best (struct dt_entry_node *dt_list);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080042
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +053043STATIC BOOLEAN DtboNeed = TRUE;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053044
lijuang6fa55c02018-06-22 19:19:26 +080045STATIC INT32 DtboIdx = INVALID_PTN;
46INT32 GetDtboIdx (VOID)
47{
48 return DtboIdx;
49}
50
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -070051STATIC INT32 DtbIdx = INVALID_PTN;
52INT32 GetDtbIdx (VOID)
53{
54 return DtbIdx;
55}
56
lijuangf0bbcad2017-08-16 16:59:18 +080057BOOLEAN GetDtboNeeded (VOID)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053058{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070059 return DtboNeed;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +053060}
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080061/* Add function to allocate dt entry list, used for recording
62 * the entry which conform to platform_dt_absolute_match()
63 */
Jeevan Shriram17f173d2017-10-24 22:11:07 -070064static struct dt_entry_node *dt_entry_list_init (VOID)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080065{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070066 struct dt_entry_node *dt_node_member = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080067
Jeevan Shriram17f173d2017-10-24 22:11:07 -070068 dt_node_member =
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +053069 (struct dt_entry_node *)AllocateZeroPool (sizeof (struct dt_entry_node));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080070
Jeevan Shriram17f173d2017-10-24 22:11:07 -070071 if (!dt_node_member) {
72 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for dt_node_member\n"));
73 return NULL;
74 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080075
Jeevan Shriram17f173d2017-10-24 22:11:07 -070076 list_clear_node (&dt_node_member->node);
77 dt_node_member->dt_entry_m =
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +053078 (struct dt_entry *)AllocateZeroPool (sizeof (struct dt_entry));
Jeevan Shriram17f173d2017-10-24 22:11:07 -070079 if (!dt_node_member->dt_entry_m) {
80 DEBUG ((EFI_D_ERROR,
81 "Failed to allocate memory for dt_node_member->dt_entry_m\n"));
82 FreePool (dt_node_member);
83 dt_node_member = NULL;
84 return NULL;
85 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080086
Jeevan Shriram17f173d2017-10-24 22:11:07 -070087 return dt_node_member;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080088}
89
Jeevan Shriram17f173d2017-10-24 22:11:07 -070090static VOID
91insert_dt_entry_in_queue (struct dt_entry_node *dt_list,
92 struct dt_entry_node *dt_node_member)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080093{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070094 list_add_tail (&dt_list->node, &dt_node_member->node);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080095}
96
Jeevan Shriram17f173d2017-10-24 22:11:07 -070097static VOID
98dt_entry_list_delete (struct dt_entry_node *dt_node_member)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -080099{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700100 if (list_in_list (&dt_node_member->node)) {
101 list_delete (&dt_node_member->node);
102 FreePool (dt_node_member->dt_entry_m);
103 dt_node_member->dt_entry_m = NULL;
104 FreePool (dt_node_member);
105 dt_node_member = NULL;
106 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800107}
108
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700109static BOOLEAN
110DeviceTreeCompatible (VOID *dtb,
111 UINT32 dtb_size,
112 struct dt_entry_node *dtb_list)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800113{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700114 int root_offset;
115 const VOID *prop = NULL;
116 const char *plat_prop = NULL;
117 const char *board_prop = NULL;
118 const char *pmic_prop = NULL;
119 char *model = NULL;
120 struct dt_entry *dt_entry_array = NULL;
121 struct board_id *board_data = NULL;
122 struct plat_id *platform_data = NULL;
123 struct pmic_id *pmic_data = NULL;
124 int len;
125 int len_board_id;
126 int len_plat_id;
127 int min_plat_id_len = 0;
128 int len_pmic_id;
129 UINT32 dtb_ver;
jianzhoue687ba82017-11-27 16:08:51 +0800130 UINT64 NumEntries = 0;
131 UINT64 i;
132 UINT32 j, k, n;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700133 UINT32 msm_data_count;
134 UINT32 board_data_count;
135 UINT32 pmic_data_count;
136 BOOLEAN Result = FALSE;
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700137 static UINT32 DtbCount;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800138
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700139 root_offset = fdt_path_offset (dtb, "/");
140 if (root_offset < 0)
141 return FALSE;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800142
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700143 prop = fdt_getprop (dtb, root_offset, "model", &len);
144 if (prop && len > 0) {
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530145 model = (char *)AllocateZeroPool (sizeof (char) * len);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700146 if (!model) {
147 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for model\n"));
148 return FALSE;
149 }
150 AsciiStrnCpyS (model, (sizeof (CHAR8) * len), prop, len);
151 } else {
152 DEBUG ((EFI_D_ERROR, "model does not exist in device tree\n"));
153 }
154 /* Find the pmic-id prop from DTB , if pmic-id is present then
155 * the DTB is version 3, otherwise find the board-id prop from DTB ,
156 * if board-id is present then the DTB is version 2 */
157 pmic_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,pmic-id",
158 &len_pmic_id);
159 board_prop = (const char *)fdt_getprop (dtb, root_offset, "qcom,board-id",
160 &len_board_id);
161 if (pmic_prop && (len_pmic_id > 0) && board_prop && (len_board_id > 0)) {
162 if ((len_pmic_id % PMIC_ID_SIZE) || (len_board_id % BOARD_ID_SIZE)) {
163 DEBUG ((EFI_D_ERROR, "qcom,pmic-id (%d) or qcom,board-id(%d) in device "
164 "tree is not a multiple of (%d %d)\n",
165 len_pmic_id, len_board_id, PMIC_ID_SIZE, BOARD_ID_SIZE));
166 goto Exit;
167 }
168 dtb_ver = DEV_TREE_VERSION_V3;
169 min_plat_id_len = PLAT_ID_SIZE;
170 } else if (board_prop && len_board_id > 0) {
171 if (len_board_id % BOARD_ID_SIZE) {
172 DEBUG ((EFI_D_ERROR,
Elliot Bermanbc287262020-01-16 09:46:53 -0800173 "qcom,board-id (%d) in device tree is not a multiple of (%d)\n",
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700174 len_board_id, BOARD_ID_SIZE));
175 goto Exit;
176 }
177 dtb_ver = DEV_TREE_VERSION_V2;
178 min_plat_id_len = PLAT_ID_SIZE;
179 } else {
180 dtb_ver = DEV_TREE_VERSION_V1;
181 min_plat_id_len = DT_ENTRY_V1_SIZE;
182 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800183
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700184 /* Get the msm-id prop from DTB */
185 plat_prop =
186 (const char *)fdt_getprop (dtb, root_offset, "qcom,msm-id", &len_plat_id);
187 if (!plat_prop || len_plat_id <= 0) {
188 DEBUG ((EFI_D_VERBOSE, "qcom,msm-id entry not found\n"));
189 goto Exit;
190 } else if (len_plat_id % min_plat_id_len) {
191 DEBUG ((EFI_D_ERROR,
192 "qcom, msm-id in device tree is (%d) not a multiple of (%d)\n",
193 len_plat_id, min_plat_id_len));
194 goto Exit;
195 }
196 if (dtb_ver == DEV_TREE_VERSION_V2 || dtb_ver == DEV_TREE_VERSION_V3) {
197 board_data_count = (len_board_id / BOARD_ID_SIZE);
198 msm_data_count = (len_plat_id / PLAT_ID_SIZE);
199 /* If dtb version is v2.0, the pmic_data_count will be <= 0 */
200 pmic_data_count = (len_pmic_id / PMIC_ID_SIZE);
201
202 /* If we are using dtb v3.0, then we have split board, msm & pmic data in
203 * the DTB
204 * If we are using dtb v2.0, then we have split board & msmdata in the DTB
205 */
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530206 board_data = (struct board_id *)AllocateZeroPool (
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700207 sizeof (struct board_id) * (len_board_id / BOARD_ID_SIZE));
208 if (!board_data) {
209 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for board_data\n"));
210 goto Exit;
211 }
212
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530213 platform_data = (struct plat_id *)AllocateZeroPool (
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700214 sizeof (struct plat_id) * (len_plat_id / PLAT_ID_SIZE));
215 if (!platform_data) {
216 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for platform_data\n"));
217 goto Exit;
218 }
219 if (dtb_ver == DEV_TREE_VERSION_V3) {
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530220 pmic_data = (struct pmic_id *)AllocateZeroPool (sizeof (struct pmic_id) *
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700221 (len_pmic_id / PMIC_ID_SIZE));
222 if (!pmic_data) {
223 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for pmic_data\n"));
lijuang46df8442017-09-28 19:06:36 +0800224 goto Exit;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700225 }
226 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800227
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700228 /* Extract board data from DTB */
229 for (i = 0; i < board_data_count; i++) {
230 board_data[i].variant_id =
231 fdt32_to_cpu (((struct board_id *)board_prop)->variant_id);
232 board_data[i].platform_subtype =
233 fdt32_to_cpu (((struct board_id *)board_prop)->platform_subtype);
234 /* For V2/V3 version of DTBs we have platform version field as part
235 * of variant ID, in such case the subtype will be mentioned as 0x0
236 * As the qcom, board-id = <0xSSPMPmPH, 0x0>
237 * SS -- Subtype
238 * PM -- Platform major version
239 * Pm -- Platform minor version
240 * PH -- Platform hardware CDP/MTP
241 * In such case to make it compatible with LK algorithm move the subtype
242 * from variant_id to subtype field
243 */
244 if (board_data[i].platform_subtype == 0)
245 board_data[i].platform_subtype =
246 fdt32_to_cpu (((struct board_id *)board_prop)->variant_id) >> 0x18;
Vijay Kumar Pendoti1e847bd2016-11-21 15:29:13 +0530247
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700248 len_board_id -= sizeof (struct board_id);
249 board_prop += sizeof (struct board_id);
250 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800251
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700252 /* Extract platform data from DTB */
253 for (i = 0; i < msm_data_count; i++) {
254 platform_data[i].platform_id =
255 fdt32_to_cpu (((struct plat_id *)plat_prop)->platform_id);
256 platform_data[i].soc_rev =
257 fdt32_to_cpu (((struct plat_id *)plat_prop)->soc_rev);
258 len_plat_id -= sizeof (struct plat_id);
259 plat_prop += sizeof (struct plat_id);
260 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800261
jianzhoue687ba82017-11-27 16:08:51 +0800262 if ((pmic_data_count != 0) &&
263 (MAX_UINT64 / pmic_data_count < (msm_data_count * board_data_count))) {
264 DEBUG ((EFI_D_ERROR, "NumEntries exceeds MAX_UINT64\n"));
265 goto Exit;
266 }
267
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700268 if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
269 /* Extract pmic data from DTB */
270 for (i = 0; i < pmic_data_count; i++) {
271 pmic_data[i].pmic_version[0] =
272 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[0]);
273 pmic_data[i].pmic_version[1] =
274 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[1]);
275 pmic_data[i].pmic_version[2] =
276 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[2]);
277 pmic_data[i].pmic_version[3] =
278 fdt32_to_cpu (((struct pmic_id *)pmic_prop)->pmic_version[3]);
279 len_pmic_id -= sizeof (struct pmic_id);
280 pmic_prop += sizeof (struct pmic_id);
281 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800282
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700283 /* We need to merge board & platform data into dt entry structure */
jianzhoue687ba82017-11-27 16:08:51 +0800284 NumEntries = (uint64_t)msm_data_count * board_data_count *
285 pmic_data_count;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700286 } else {
287 /* We need to merge board & platform data into dt entry structure */
jianzhoue687ba82017-11-27 16:08:51 +0800288 NumEntries = (uint64_t)msm_data_count * board_data_count;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700289 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800290
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530291 dt_entry_array = (struct dt_entry *)AllocateZeroPool (
292 sizeof (struct dt_entry) *
293 NumEntries);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700294 if (!dt_entry_array) {
295 DEBUG ((EFI_D_ERROR, "Failed to allocate memory for dt_entry_array\n"));
296 goto Exit;
297 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800298
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700299 /* If we have '<X>; <Y>; <Z>' as platform data & '<A>; <B>; <C>' as board
300 * data.
301 * Then dt entry should look like
302 * <X ,A >;<X, B>;<X, C>;
303 * <Y ,A >;<Y, B>;<Y, C>;
304 * <Z ,A >;<Z, B>;<Z, C>;
305 */
306 k = 0;
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700307 DtbCount++;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700308 for (i = 0; i < msm_data_count; i++) {
309 for (j = 0; j < board_data_count; j++) {
310 if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
311 for (n = 0; n < pmic_data_count; n++) {
312 dt_entry_array[k].platform_id = platform_data[i].platform_id;
313 dt_entry_array[k].soc_rev = platform_data[i].soc_rev;
314 dt_entry_array[k].variant_id = board_data[j].variant_id;
315 dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype;
316 dt_entry_array[k].pmic_rev[0] = pmic_data[n].pmic_version[0];
317 dt_entry_array[k].pmic_rev[1] = pmic_data[n].pmic_version[1];
318 dt_entry_array[k].pmic_rev[2] = pmic_data[n].pmic_version[2];
319 dt_entry_array[k].pmic_rev[3] = pmic_data[n].pmic_version[3];
320 dt_entry_array[k].offset = (UINT64)dtb;
321 dt_entry_array[k].size = dtb_size;
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700322 dt_entry_array[k].Idx = DtbCount;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700323 k++;
324 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800325
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700326 } else {
327 dt_entry_array[k].platform_id = platform_data[i].platform_id;
328 dt_entry_array[k].soc_rev = platform_data[i].soc_rev;
329 dt_entry_array[k].variant_id = board_data[j].variant_id;
330 dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype;
331 dt_entry_array[k].pmic_rev[0] = BoardPmicTarget (0);
332 dt_entry_array[k].pmic_rev[1] = BoardPmicTarget (1);
333 dt_entry_array[k].pmic_rev[2] = BoardPmicTarget (2);
334 dt_entry_array[k].pmic_rev[3] = BoardPmicTarget (3);
335 dt_entry_array[k].offset = (UINT64)dtb;
336 dt_entry_array[k].size = dtb_size;
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700337 dt_entry_array[k].Idx = DtbCount;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700338 k++;
lijuang46df8442017-09-28 19:06:36 +0800339 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700340 }
341 }
342
jianzhoue687ba82017-11-27 16:08:51 +0800343 for (i = 0; i < NumEntries; i++) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700344 if (platform_dt_absolute_match (&(dt_entry_array[i]), dtb_list)) {
345 DEBUG ((EFI_D_VERBOSE, "Device tree exact match the board: <0x%x 0x%x "
346 "0x%x 0x%x> == <0x%x 0x%x 0x%x 0x%x>\n",
347 dt_entry_array[i].platform_id, dt_entry_array[i].variant_id,
348 dt_entry_array[i].soc_rev, dt_entry_array[i].board_hw_subtype,
349 BoardPlatformRawChipId (), BoardPlatformType (),
350 BoardPlatformChipVersion (), BoardPlatformSubType ()));
351 } else {
352 DEBUG ((EFI_D_VERBOSE, "Device tree's msm_id doesn't match the board: "
353 "<0x%x 0x%x 0x%x 0x%x> != <0x%x 0x%x 0x%x "
354 "0x%x>\n",
355 dt_entry_array[i].platform_id, dt_entry_array[i].variant_id,
356 dt_entry_array[i].soc_rev, dt_entry_array[i].board_hw_subtype,
357 BoardPlatformRawChipId (), BoardPlatformType (),
358 BoardPlatformChipVersion (), BoardPlatformSubType ()));
359 }
360 }
361 Result = TRUE;
362 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800363
lijuang46df8442017-09-28 19:06:36 +0800364Exit:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700365 if (board_data) {
366 FreePool (board_data);
367 board_data = NULL;
368 }
369 if (platform_data) {
370 FreePool (platform_data);
371 platform_data = NULL;
372 }
373 if (pmic_data) {
374 FreePool (pmic_data);
375 pmic_data = NULL;
376 }
377 if (dt_entry_array) {
378 FreePool (dt_entry_array);
379 dt_entry_array = NULL;
380 }
381 if (model) {
382 FreePool (model);
383 model = NULL;
384 }
lijuang46df8442017-09-28 19:06:36 +0800385
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700386 return Result;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800387}
388
389/*
390 * Will relocate the DTB to the tags addr if the device tree is found and return
391 * its address
392 *
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700393 * For Header Version 2, the arguments Kernel and KernelSize will be
394 * the entire bootimage and the bootimage size.
395 *
396 * Arguments: kernel - Start address of the kernel/bootimage
397 * loaded in RAM
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800398 * tags - Start address of the tags loaded in RAM
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700399 * kernel_size - Size of the kernel/bootimage in bytes
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800400 *
401 * Return Value: DTB address : If appended device tree is found
402 * 'NULL' : Otherwise
403 */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700404VOID *
405DeviceTreeAppended (VOID *kernel,
406 UINT32 kernel_size,
407 UINT32 dtb_offset,
408 VOID *tags)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800409{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700410 EFI_STATUS Status;
411 uintptr_t kernel_end = (uintptr_t)kernel + kernel_size;
412 VOID *dtb = NULL;
413 VOID *bestmatch_tag = NULL;
414 UINT64 RamdiskLoadAddr;
415 UINT64 BaseMemory = 0;
416 struct dt_entry *best_match_dt_entry = NULL;
417 UINT32 bestmatch_tag_size;
418 struct dt_entry_node *dt_entry_queue = NULL;
419 struct dt_entry_node *dt_node_tmp1 = NULL;
420 struct dt_entry_node *dt_node_tmp2 = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800421
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700422 /* Initialize the dtb entry node*/
423 dt_entry_queue =
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +0530424 (struct dt_entry_node *)AllocateZeroPool (sizeof (struct dt_entry_node));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700425 if (!dt_entry_queue) {
426 DEBUG ((EFI_D_ERROR, "Out of memory\n"));
427 return NULL;
428 }
Jeevan Shrirame07dd432016-08-22 11:05:06 -0700429
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700430 list_initialize (&dt_entry_queue->node);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800431
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700432 if (!dtb_offset) {
433 DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n"));
434 goto out;
435 }
Runmin Wang7db42962016-06-27 14:35:35 -0700436
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700437 if (((uintptr_t)kernel + (uintptr_t)dtb_offset) < (uintptr_t)kernel) {
438 goto out;
439 }
440 dtb = kernel + dtb_offset;
441 while (((uintptr_t)dtb + sizeof (struct fdt_header)) <
442 (uintptr_t)kernel_end) {
443 struct fdt_header dtb_hdr;
444 UINT32 dtb_size;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800445
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700446 /* the DTB could be unaligned, so extract the header,
447 * and operate on it separately */
448 gBS->CopyMem (&dtb_hdr, dtb, sizeof (struct fdt_header));
449 if (fdt_check_header ((const VOID *)&dtb_hdr) != 0 ||
450 fdt_check_header_ext ((VOID *)&dtb_hdr) != 0 ||
451 ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) <
452 (uintptr_t)dtb) ||
453 ((uintptr_t)dtb + (uintptr_t)fdt_totalsize ((const VOID *)&dtb_hdr) >
454 (uintptr_t)kernel_end))
455 break;
456 dtb_size = fdt_totalsize (&dtb_hdr);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800457
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700458 if (!DeviceTreeCompatible (dtb, dtb_size, dt_entry_queue)) {
459 DEBUG ((EFI_D_VERBOSE, "Error while DTB parse continue with next DTB\n"));
460 if (!GetRticDtb (dtb))
461 DEBUG ((EFI_D_VERBOSE,
462 "Error while DTB parsing RTIC prop continue with next DTB\n"));
463 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +0530464
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700465 /* goto the next device tree if any */
466 dtb += dtb_size;
467 }
468 best_match_dt_entry = platform_dt_match_best (dt_entry_queue);
469 if (best_match_dt_entry) {
470 bestmatch_tag = (VOID *)best_match_dt_entry->offset;
471 bestmatch_tag_size = best_match_dt_entry->size;
472 DEBUG ((EFI_D_INFO, "Best match DTB tags "
473 "%u/%08x/0x%08x/%x/%x/%x/%x/%x/(offset)0x%08x/"
474 "(size)0x%08x\n",
475 best_match_dt_entry->platform_id, best_match_dt_entry->variant_id,
476 best_match_dt_entry->board_hw_subtype, best_match_dt_entry->soc_rev,
477 best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
478 best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
479 best_match_dt_entry->offset, best_match_dt_entry->size));
480 DEBUG ((EFI_D_INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device "
481 "0x%0x/0x%x/0x%x/0x%0x\n",
482 best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
483 best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
484 BoardPmicTarget (0), BoardPmicTarget (1), BoardPmicTarget (2),
485 BoardPmicTarget (3)));
486 }
487 /* free queue's memory */
488 list_for_every_entry (&dt_entry_queue->node, dt_node_tmp1, dt_node, node)
489 {
490 if (!dt_node_tmp1) {
491 DEBUG ((EFI_D_VERBOSE, "Current node is the end\n"));
492 break;
493 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800494
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700495 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
496 dt_entry_list_delete (dt_node_tmp1);
497 dt_node_tmp1 = dt_node_tmp2;
498 }
lijuangf71a4842017-07-27 19:26:31 +0800499
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700500 if (bestmatch_tag) {
501 Status = BaseMem (&BaseMemory);
502 if (Status != EFI_SUCCESS) {
503 DEBUG ((EFI_D_ERROR, "Unable to find Base memory for DDR %r\n", Status));
504 FreePool (dt_entry_queue);
505 dt_entry_queue = NULL;
506 goto out;
507 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800508
Venkata Narendra Kumar Gutta00f46852018-11-29 12:38:08 -0800509 RamdiskLoadAddr = SetandGetLoadAddr (NULL, LOAD_ADDR_RAMDISK);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700510 if ((RamdiskLoadAddr - (UINT64)tags) > RamdiskLoadAddr) {
511 DEBUG ((EFI_D_ERROR, "Tags address is not valid\n"));
512 goto out;
513 }
514 if ((RamdiskLoadAddr - (UINT64)tags) < bestmatch_tag_size) {
515 DEBUG ((EFI_D_ERROR, "Tag size is over the limit\n"));
516 goto out;
517 }
518 gBS->CopyMem (tags, bestmatch_tag, bestmatch_tag_size);
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700519 DtbIdx = best_match_dt_entry->Idx;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700520 /* clear out the old DTB magic so kernel doesn't find it */
521 *((UINT32 *)(kernel + dtb_offset)) = 0;
lijuang46df8442017-09-28 19:06:36 +0800522 FreePool (dt_entry_queue);
523 dt_entry_queue = NULL;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700524
525 return tags;
526 }
527
528 DEBUG (
529 (EFI_D_ERROR,
530 "DTB offset is incorrect, kernel image does not have appended DTB\n"));
531
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700532out:
533 FreePool (dt_entry_queue);
534 dt_entry_queue = NULL;
535 return NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -0800536}
537
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700538STATIC BOOLEAN
Lijuan Gao1259da72021-03-05 10:39:12 +0800539CheckAllBitsSet (UINT64 DtMatchVal)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530540{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700541 return (DtMatchVal & ALL_BITS_SET) == (ALL_BITS_SET);
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530542}
543
Mukesh Ojhafa815352017-11-08 14:43:41 +0530544STATIC VOID
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530545ReadBestPmicMatch (CONST CHAR8 *PmicProp, INT32 PmicMaxIdx,
546 UINT32 PmicEntCount, PmicIdInfo *BestPmicInfo)
Mukesh Ojhafa815352017-11-08 14:43:41 +0530547{
548 UINT32 PmicEntIdx;
549 UINT32 Idx;
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530550 PmicIdInfo CurPmicInfo;
Mukesh Ojhafa815352017-11-08 14:43:41 +0530551
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530552 memset (BestPmicInfo, 0, sizeof (PmicIdInfo));
Mukesh Ojhafa815352017-11-08 14:43:41 +0530553 for (PmicEntIdx = 0; PmicEntIdx < PmicEntCount; PmicEntIdx++) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530554 memset (&CurPmicInfo, 0, sizeof (PmicIdInfo));
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530555 for (Idx = 0; Idx < PmicMaxIdx; Idx++) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530556 CurPmicInfo.DtPmicModel[Idx] =
Mukesh Ojhafa815352017-11-08 14:43:41 +0530557 fdt32_to_cpu (((struct pmic_id *)PmicProp)->pmic_version[Idx]);
558
559 DEBUG ((EFI_D_VERBOSE, "pmic_data[%u].:%x\n", Idx,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530560 CurPmicInfo.DtPmicModel[Idx]));
561 CurPmicInfo.DtPmicRev[Idx] =
562 CurPmicInfo.DtPmicModel[Idx] & PMIC_REV_MASK;
563 CurPmicInfo.DtPmicModel[Idx] =
564 CurPmicInfo.DtPmicModel[Idx] & PMIC_MODEL_MASK;
Mukesh Ojhafa815352017-11-08 14:43:41 +0530565
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530566 if ((CurPmicInfo.DtPmicModel[Idx]) ==
Mukesh Ojhafa815352017-11-08 14:43:41 +0530567 BoardPmicModel (Idx)) {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530568 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530569 BIT ((PMIC_MATCH_EXACT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX));
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530570 } else if (CurPmicInfo.DtPmicModel[Idx] == 0) {
571 CurPmicInfo.DtMatchVal |=
Mukesh Ojhafa815352017-11-08 14:43:41 +0530572 BIT ((PMIC_MATCH_DEFAULT_MODEL_IDX0 + Idx * PMIC_SHIFT_IDX));
573 } else {
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530574 CurPmicInfo.DtMatchVal = BIT (NONE_MATCH);
Mukesh Ojhafa815352017-11-08 14:43:41 +0530575 DEBUG ((EFI_D_VERBOSE, "Pmic model does not match\n"));
576 break;
577 }
578
Lijuan Gao1259da72021-03-05 10:39:12 +0800579 /* first match the first four pmic revision */
580 if (Idx < PMIC_IDX4) {
581 if (CurPmicInfo.DtPmicRev[Idx] == (BoardPmicTarget (Idx)
582 & PMIC_REV_MASK)) {
583 CurPmicInfo.DtMatchVal |=
584 BIT ((PMIC_MATCH_EXACT_REV_IDX0 + Idx * PMIC_SHIFT_IDX));
585 } else if (CurPmicInfo.DtPmicRev[Idx] <
586 (BoardPmicTarget (Idx) & PMIC_REV_MASK)) {
587 CurPmicInfo.DtMatchVal |= BIT ((PMIC_MATCH_BEST_REV_IDX0 +
588 Idx * PMIC_SHIFT_IDX));
589 } else {
590 DEBUG ((EFI_D_VERBOSE, "Pmic revision does not match\n"));
591 break;
592 }
Mukesh Ojhafa815352017-11-08 14:43:41 +0530593 }
594 }
595
Lijuan Gao1259da72021-03-05 10:39:12 +0800596 DEBUG ((EFI_D_VERBOSE, "BestPmicInfo.DtMatchVal : 0x%llx"
597 " CurPmicInfo[%u]->DtMatchVal : 0x%llx\n", BestPmicInfo->DtMatchVal,
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530598 PmicEntIdx, CurPmicInfo.DtMatchVal));
599 if (BestPmicInfo->DtMatchVal < CurPmicInfo.DtMatchVal) {
600 gBS->CopyMem (BestPmicInfo, &CurPmicInfo,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530601 sizeof (struct PmicIdInfo));
602 } else if (BestPmicInfo->DtMatchVal ==
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530603 CurPmicInfo.DtMatchVal) {
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530604 for (Idx = 0; Idx < PmicMaxIdx; Idx++) {
605 if (BestPmicInfo->DtPmicRev[Idx] < CurPmicInfo.DtPmicRev[Idx]) {
606 gBS->CopyMem (BestPmicInfo, &CurPmicInfo, sizeof (struct PmicIdInfo));
607 }
Mukesh Ojhafa815352017-11-08 14:43:41 +0530608 }
609 }
610
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530611 PmicProp += sizeof (UINT32) * PmicMaxIdx;
Mukesh Ojhafa815352017-11-08 14:43:41 +0530612 }
613}
614
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800615STATIC EFI_STATUS GetPlatformMatchDtb (DtInfo * CurDtbInfo,
616 CONST CHAR8 *PlatProp,
617 INT32 LenPlatId,
618 INT32 MinPlatIdLen)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530619{
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530620
lijuangf6b1c5f2018-03-20 13:15:47 +0800621 if (CurDtbInfo == NULL) {
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800622 DEBUG ((EFI_D_VERBOSE, "Input parameters null\n"));
623 return EFI_INVALID_PARAMETER;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700624 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530625
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700626 if (PlatProp && (LenPlatId > 0) && (!(LenPlatId % MinPlatIdLen))) {
627 /*Compare msm-id of the dtb vs Board*/
628 CurDtbInfo->DtPlatformId =
629 fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id);
630 DEBUG ((EFI_D_VERBOSE, "Boardsocid = %x, Dtsocid = %x\n",
631 (BoardPlatformRawChipId () & SOC_MASK),
632 (CurDtbInfo->DtPlatformId & SOC_MASK)));
633 if ((BoardPlatformRawChipId () & SOC_MASK) ==
634 (CurDtbInfo->DtPlatformId & SOC_MASK)) {
635 CurDtbInfo->DtMatchVal |= BIT (SOC_MATCH);
636 } else {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530637 DEBUG ((EFI_D_VERBOSE, "qcom,msm-id does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700638 /* If it's neither exact nor default match don't select dtb */
639 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800640 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700641 }
642 /*Compare soc rev of the dtb vs Board*/
643 CurDtbInfo->DtSocRev = fdt32_to_cpu (((struct plat_id *)PlatProp)->soc_rev);
644 DEBUG ((EFI_D_VERBOSE, "BoardSocRev = %x, DtSocRev =%x\n",
645 BoardPlatformChipVersion (), CurDtbInfo->DtSocRev));
646 if (CurDtbInfo->DtSocRev == BoardPlatformChipVersion ()) {
647 CurDtbInfo->DtMatchVal |= BIT (VERSION_EXACT_MATCH);
648 } else if (CurDtbInfo->DtSocRev < BoardPlatformChipVersion ()) {
649 CurDtbInfo->DtMatchVal |= BIT (VERSION_BEST_MATCH);
650 } else if (CurDtbInfo->DtSocRev) {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530651 DEBUG ((EFI_D_VERBOSE, "soc version does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700652 }
653 /*Compare Foundry Id of the dtb vs Board*/
654 CurDtbInfo->DtFoundryId =
655 fdt32_to_cpu (((struct plat_id *)PlatProp)->platform_id) &
656 FOUNDRY_ID_MASK;
657 DEBUG ((EFI_D_VERBOSE, "BoardFoundry = %x, DtFoundry = %x\n",
658 (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT),
659 CurDtbInfo->DtFoundryId));
660 if (CurDtbInfo->DtFoundryId ==
661 (BoardPlatformFoundryId () << PLATFORM_FOUNDRY_SHIFT)) {
662 CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_EXACT_MATCH);
663 } else if (CurDtbInfo->DtFoundryId == 0) {
664 CurDtbInfo->DtMatchVal |= BIT (FOUNDRYID_DEFAULT_MATCH);
665 } else {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530666 DEBUG ((EFI_D_VERBOSE, "soc foundry does not match\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700667 /* If it's neither exact nor default match don't select dtb */
668 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800669 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700670 }
671 } else {
672 DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is"
673 " (%d) not a multiple of (%d)\n",
674 LenPlatId, MinPlatIdLen));
675 }
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800676 return EFI_SUCCESS;
677}
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530678
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800679STATIC EFI_STATUS GetBoardMatchDtb (DtInfo *CurDtbInfo,
680 CONST CHAR8 *BoardProp,
681 INT32 LenBoardId)
682{
lijuangf6b1c5f2018-03-20 13:15:47 +0800683 if (CurDtbInfo == NULL) {
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800684 DEBUG ((EFI_D_VERBOSE, "Input parameters null\n"));
685 return EFI_INVALID_PARAMETER;
686 }
687
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700688 if (BoardProp && (LenBoardId > 0) && (!(LenBoardId % BOARD_ID_SIZE))) {
689 CurDtbInfo->DtVariantId =
690 fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id);
691 CurDtbInfo->DtPlatformSubtype =
692 fdt32_to_cpu (((struct board_id *)BoardProp)->platform_subtype);
693 if (CurDtbInfo->DtPlatformSubtype == 0) {
694 CurDtbInfo->DtPlatformSubtype =
695 fdt32_to_cpu (((struct board_id *)BoardProp)->variant_id) >>
696 PLATFORM_SUBTYPE_SHIFT_ID;
697 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530698
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700699 DEBUG ((EFI_D_VERBOSE, "BoardVariant = %x, DtVariant = %x\n",
700 BoardPlatformType (), CurDtbInfo->DtVariantId));
701 CurDtbInfo->DtVariantMajor = CurDtbInfo->DtVariantId & VARIANT_MAJOR_MASK;
702 CurDtbInfo->DtVariantMinor = CurDtbInfo->DtVariantId & VARIANT_MINOR_MASK;
703 CurDtbInfo->DtVariantId = CurDtbInfo->DtVariantId & VARIANT_MASK;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530704
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700705 if (CurDtbInfo->DtVariantId == BoardPlatformType ()) {
706 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MATCH);
707 } else if (CurDtbInfo->DtVariantId) {
708 DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not"
709 " match\n"));
710 /* If it's neither exact nor default match don't select dtb */
711 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800712 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700713 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +0530714
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700715 if (CurDtbInfo->DtVariantMajor == (BoardTargetId () & VARIANT_MAJOR_MASK)) {
716 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_EXACT_MATCH);
717 } else if (CurDtbInfo->DtVariantMajor <
718 (BoardTargetId () & VARIANT_MAJOR_MASK)) {
719 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MAJOR_BEST_MATCH);
720 } else if (CurDtbInfo->DtVariantMajor) {
721 DEBUG ((EFI_D_VERBOSE, "qcom,board-id major version "
722 "does not match\n"));
723 }
Parth Dixitd4887652017-09-13 22:28:52 +0530724
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700725 if (CurDtbInfo->DtVariantMinor == (BoardTargetId () & VARIANT_MINOR_MASK)) {
726 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_EXACT_MATCH);
727 } else if (CurDtbInfo->DtVariantMinor <
728 (BoardTargetId () & VARIANT_MINOR_MASK)) {
729 CurDtbInfo->DtMatchVal |= BIT (VARIANT_MINOR_BEST_MATCH);
730 } else if (CurDtbInfo->DtVariantMinor) {
731 DEBUG ((EFI_D_VERBOSE, "qcom,board-id minor version "
732 "does not match\n"));
733 }
Parth Dixitd4887652017-09-13 22:28:52 +0530734
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700735 DEBUG ((EFI_D_VERBOSE, "BoardSubtype = %x, DtSubType = %x\n",
736 BoardPlatformSubType (), CurDtbInfo->DtPlatformSubtype));
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +0530737 if ((CurDtbInfo->DtPlatformSubtype & PLATFORM_SUBTYPE_MASK) ==
738 BoardPlatformSubType ()) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700739 CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_EXACT_MATCH);
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +0530740 } else if ((CurDtbInfo->DtPlatformSubtype & PLATFORM_SUBTYPE_MASK) == 0) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700741 CurDtbInfo->DtMatchVal |= BIT (SUBTYPE_DEFAULT_MATCH);
jianzhou254e1e02017-11-01 14:52:02 +0800742 } else {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700743 DEBUG ((EFI_D_VERBOSE, "subtype-id doesnot match\n"));
744 /* If it's neither exact nor default match don't select dtb */
745 CurDtbInfo->DtMatchVal = BIT (NONE_MATCH);
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800746 return EFI_NOT_FOUND;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700747 }
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +0530748
749 if ((CurDtbInfo->DtPlatformSubtype & DDR_MASK) ==
750 (BoardPlatformHlosSubType() & DDR_MASK)) {
751 CurDtbInfo->DtMatchVal |= BIT (DDR_MATCH);
752 } else {
753 DEBUG ((EFI_D_VERBOSE, "ddr size does not match\n"));
754 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700755 } else {
756 DEBUG ((EFI_D_VERBOSE, "qcom,board-id does not exist (or) (%d) "
757 "is not a multiple of (%d)\n",
758 LenBoardId, BOARD_ID_SIZE));
759 }
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800760 return EFI_SUCCESS;
761}
762
763/* Dt selection table for quick reference
764 | SNO | Dt Property | CDT Property | Exact | Best | Default |
765 |-----+---------------+-----------------+-------+------+---------+
766 | | qcom, msm-id | | | | |
767 | | | PlatformId | Y | N | N |
768 | | | SocRev | N | Y | N |
769 | | | FoundryId | Y | N | 0 |
770 | | qcom,board-id | | | | |
771 | | | VariantId | Y | N | N |
772 | | | VariantMajor | N | Y | N |
773 | | | VariantMinor | N | Y | N |
774 | | | PlatformSubtype | Y | N | 0 |
775 | | qcom,pmic-id | | | | |
776 | | | PmicModelId | Y | N | 0 |
777 | | | PmicMetalRev | N | Y | N |
778 | | | PmicLayerRev | N | Y | N |
779 | | | PmicVariantRev | N | Y | N |
780*/
lijuang6fa55c02018-06-22 19:19:26 +0800781STATIC BOOLEAN
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800782ReadDtbFindMatch (DtInfo *CurDtbInfo, DtInfo *BestDtbInfo, UINT32 ExactMatch)
783{
784 EFI_STATUS Status;
785 CONST CHAR8 *PlatProp = NULL;
786 CONST CHAR8 *BoardProp = NULL;
787 CONST CHAR8 *PmicProp = NULL;
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530788 CONST CHAR8 *PmicPropSz = NULL;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800789 INT32 LenBoardId;
790 INT32 LenPlatId;
791 INT32 LenPmicId;
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530792 INT32 LenPmicIdSz;
793 INT32 PmicMaxIdx;
794 INT32 PmicEntSz;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800795 INT32 MinPlatIdLen = PLAT_ID_SIZE;
796 INT32 RootOffset = 0;
797 VOID *Dtb = CurDtbInfo->Dtb;
798 UINT32 Idx;
799 UINT32 PmicEntCount;
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530800 UINT32 MsmDataCount;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800801 PmicIdInfo BestPmicInfo;
lijuang6fa55c02018-06-22 19:19:26 +0800802 BOOLEAN FindBestMatch = FALSE;
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530803 DtInfo TempDtbInfo = *CurDtbInfo;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800804
805 memset (&BestPmicInfo, 0, sizeof (PmicIdInfo));
806 /*Ensure MatchVal to 0 initially*/
807 CurDtbInfo->DtMatchVal = 0;
808 RootOffset = fdt_path_offset (Dtb, "/");
809 if (RootOffset < 0) {
810 DEBUG ((EFI_D_ERROR, "Unable to locate root node\n"));
lijuang6fa55c02018-06-22 19:19:26 +0800811 return FALSE;
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800812 }
813
814 /* Get the msm-id prop from DTB */
815 PlatProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,msm-id",
816 &LenPlatId);
Bhanuprakash Modem64333302018-09-10 18:58:11 +0530817 if (PlatProp &&
818 (LenPlatId > 0) &&
819 (!(LenPlatId % MinPlatIdLen))) {
820 /*
821 For Multiple soc-id's, save the best SocRev match DT in temp and search
822 for the exact match. If exact match is not found, use best match DT from
823 temp.
824 */
825 MsmDataCount = (LenPlatId / MinPlatIdLen);
826
827 /* Ensure to reset the match value */
828 TempDtbInfo.DtMatchVal = NONE_MATCH;
829
830 for (Idx = 0; Idx < MsmDataCount; Idx++) {
831 /* Ensure MatchVal should be 0 for every match */
832 CurDtbInfo->DtMatchVal = NONE_MATCH;
833 Status = GetPlatformMatchDtb (CurDtbInfo,
834 PlatProp,
835 LenPlatId,
836 MinPlatIdLen);
837 if (Status == EFI_SUCCESS &&
838 CurDtbInfo->DtMatchVal > TempDtbInfo.DtMatchVal) {
839 TempDtbInfo = *CurDtbInfo;
840 }
841 LenPlatId -= PLAT_ID_SIZE;
842 PlatProp += PLAT_ID_SIZE;
843 }
844
845 *CurDtbInfo = TempDtbInfo;
846
847 if (CurDtbInfo->DtMatchVal == NONE_MATCH) {
848 DEBUG ((EFI_D_VERBOSE, "Platform dt prop search failed.\n"));
849 goto cleanup;
850 }
851 } else {
852 DEBUG ((EFI_D_VERBOSE, "qcom, msm-id does not exist (or) is"
853 " (%d) not a multiple of (%d)\n",
854 LenPlatId, MinPlatIdLen));
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800855 }
856
857 /* Get the properties like variant id, subtype from Dtb then compare the
858 * dtb vs Board*/
859 BoardProp = (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,board-id",
860 &LenBoardId);
861 Status = GetBoardMatchDtb (CurDtbInfo, BoardProp, LenBoardId);
862 if (Status != EFI_SUCCESS) {
Mukesh Ojha0eedc352018-02-14 17:42:30 +0530863 DEBUG ((EFI_D_VERBOSE, "Board dt prop search failed.\n"));
Kamalpreet Singhe3b8fe42017-12-07 14:03:36 -0800864 goto cleanup;
865 }
Parth Dixitd4887652017-09-13 22:28:52 +0530866
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700867 /*Get the pmic property from Dtb then compare the dtb vs Board*/
868 PmicProp =
869 (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,pmic-id", &LenPmicId);
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530870
871 if ((PmicProp) &&
872 (LenPmicId > 0)) {
873 PmicPropSz =
874 (CONST CHAR8 *)fdt_getprop (Dtb, RootOffset, "qcom,pmic-id-size",
875 &LenPmicIdSz);
876 if ((PmicPropSz) &&
877 (LenPmicIdSz > 0)) {
878 PmicMaxIdx = (fdt32_to_cpu (*((UINT32 *)PmicPropSz)));
879 } else {
880 /* By default support four pmic, qcom,pmic-id = <a, b, c, d>*/
881 PmicMaxIdx = PMIC_IDX4;
882 }
883
884 PmicEntSz = PmicMaxIdx * sizeof (UINT32);
885 if (LenPmicId % PmicEntSz) {
886 DEBUG ((EFI_D_VERBOSE,
887 "LenPmicId(%d) is not multiple of PmicEntSz(%d)\n",
888 LenPmicId, PmicEntSz));
889 goto cleanup;
890 }
891
892 PmicEntCount = LenPmicId / PmicEntSz;
Mukesh Ojha8ff5b4a2017-12-28 17:47:19 +0530893 /* Get the best match pmic */
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530894 ReadBestPmicMatch (PmicProp, PmicMaxIdx, PmicEntCount, &BestPmicInfo);
Mukesh Ojhafa815352017-11-08 14:43:41 +0530895 CurDtbInfo->DtMatchVal |= BestPmicInfo.DtMatchVal;
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530896 for (Idx = 0; Idx < PmicMaxIdx; Idx++) {
Mukesh Ojhafa815352017-11-08 14:43:41 +0530897 CurDtbInfo->DtPmicModel[Idx] = BestPmicInfo.DtPmicModel[Idx];
898 CurDtbInfo->DtPmicRev[Idx] = BestPmicInfo.DtPmicRev[Idx];
899 }
900
Lijuan Gao1259da72021-03-05 10:39:12 +0800901 DEBUG ((EFI_D_VERBOSE, "CurDtbInfo->DtMatchVal : 0x%llx "
902 "BestPmicInfo.DtMatchVal :0x%llx\n", CurDtbInfo->DtMatchVal,
Mukesh Ojhafa815352017-11-08 14:43:41 +0530903 BestPmicInfo.DtMatchVal));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700904 } else {
Mayank Groverc8ad1e72020-12-02 15:14:00 +0530905 DEBUG ((EFI_D_VERBOSE, "qcom,pmic-id does not exit\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700906 }
Parth Dixitd4887652017-09-13 22:28:52 +0530907
908cleanup:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700909 if (CurDtbInfo->DtMatchVal & BIT (ExactMatch)) {
910 if (BestDtbInfo->DtMatchVal < CurDtbInfo->DtMatchVal) {
911 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
lijuang6fa55c02018-06-22 19:19:26 +0800912 FindBestMatch = TRUE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700913 } else if (BestDtbInfo->DtMatchVal == CurDtbInfo->DtMatchVal) {
lijuang6fa55c02018-06-22 19:19:26 +0800914 FindBestMatch = TRUE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700915 if (BestDtbInfo->DtSocRev < CurDtbInfo->DtSocRev) {
916 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
917 } else if (BestDtbInfo->DtVariantMajor < CurDtbInfo->DtVariantMajor) {
918 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
919 } else if (BestDtbInfo->DtVariantMinor < CurDtbInfo->DtVariantMinor) {
920 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
921 } else if (BestDtbInfo->DtPmicRev[0] < CurDtbInfo->DtPmicRev[0]) {
922 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
923 } else if (BestDtbInfo->DtPmicRev[1] < CurDtbInfo->DtPmicRev[1]) {
924 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
925 } else if (BestDtbInfo->DtPmicRev[2] < CurDtbInfo->DtPmicRev[2]) {
926 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
927 } else if (BestDtbInfo->DtPmicRev[3] < CurDtbInfo->DtPmicRev[3]) {
928 gBS->CopyMem (BestDtbInfo, CurDtbInfo, sizeof (struct DtInfo));
lijuang6fa55c02018-06-22 19:19:26 +0800929 } else {
930 FindBestMatch = FALSE;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700931 }
932 }
933 }
lijuang6fa55c02018-06-22 19:19:26 +0800934
935 return FindBestMatch;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700936}
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700937/*
938 * For Header Version 2, the arguments Kernel and KernelSize will be
939 * the entire bootimage and the bootimage size.
Raghavendra Rao Anantac3668a22020-01-13 18:20:02 -0800940 * For Header Version 3, Kernel holds the base of the vendor_boot
Raghavendra Rao Ananta9ba062d2019-10-12 11:26:33 -0700941 * image and KernelSize holds its size.
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700942 */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700943VOID *
944GetSocDtb (VOID *Kernel, UINT32 KernelSize, UINT32 DtbOffset, VOID *DtbLoadAddr)
945{
946 uintptr_t KernelEnd = (uintptr_t)Kernel + KernelSize;
947 VOID *Dtb = NULL;
948 struct fdt_header DtbHdr;
949 UINT32 DtbSize = 0;
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700950 INT32 DtbCount = 0;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700951 DtInfo CurDtbInfo = {0};
952 DtInfo BestDtbInfo = {0};
953 if (!DtbOffset) {
954 DEBUG ((EFI_D_ERROR, "DTB offset is NULL\n"));
955 return NULL;
956 }
957
958 if (((uintptr_t)Kernel + (uintptr_t)DtbOffset) < (uintptr_t)Kernel) {
959 return NULL;
960 }
961 Dtb = Kernel + DtbOffset;
962 while (((uintptr_t)Dtb + sizeof (struct fdt_header)) < (uintptr_t)KernelEnd) {
963 /* the DTB could be unaligned, so extract the header,
964 * and operate on it separately */
965 gBS->CopyMem (&DtbHdr, Dtb, sizeof (struct fdt_header));
966 DtbSize = fdt_totalsize ((const VOID *)&DtbHdr);
967 if (fdt_check_header ((const VOID *)&DtbHdr) != 0 ||
968 fdt_check_header_ext ((VOID *)&DtbHdr) != 0 ||
969 ((uintptr_t)Dtb + DtbSize < (uintptr_t)Dtb) ||
970 ((uintptr_t)Dtb + DtbSize > (uintptr_t)KernelEnd))
971 break;
972
973 CurDtbInfo.Dtb = Dtb;
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700974 if (ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, SOC_MATCH)) {
975 DtbIdx = DtbCount;
976 }
977
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700978 if (CurDtbInfo.DtMatchVal) {
979 if (CurDtbInfo.DtMatchVal & BIT (SOC_MATCH)) {
980 if (CheckAllBitsSet (CurDtbInfo.DtMatchVal)) {
981 DEBUG ((EFI_D_VERBOSE, "Exact DTB match"
982 " found. DTBO search is not "
983 "required\n"));
984 DtboNeed = FALSE;
Parth Dixitd4887652017-09-13 22:28:52 +0530985 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700986 }
987 } else {
988 if (!GetRticDtb (Dtb)) {
989 DEBUG ((EFI_D_VERBOSE, "Error while DTB parsing"
990 " RTIC prop continue with next DTB\n"));
991 }
992 }
993
994 DEBUG ((EFI_D_VERBOSE, "Bestmatch = %x\n", BestDtbInfo.DtMatchVal));
995 Dtb += DtbSize;
Prakruthi Deepak Heragu9c385aa2019-03-20 10:54:29 -0700996 DtbCount++;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700997 }
998
999 if (!BestDtbInfo.Dtb) {
1000 DEBUG ((EFI_D_ERROR, "No match found for Soc Dtb type\n"));
1001 return NULL;
1002 }
1003
1004 return BestDtbInfo.Dtb;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301005}
1006
Mayank Grover98dcd672019-02-05 14:46:25 +05301007/*
1008 Function to extract Dtb from user dtbo partition.
1009*/
1010EFI_STATUS
1011GetOvrdDtb ( VOID **DtboImgBuffer)
1012{
1013 struct DtboTableHdr *DtboTableHdr = NULL;
1014 struct DtboTableEntry *DtboTableEntry = NULL;
1015 VOID *OvrdDtb = NULL;
1016 UINT32 DtboTableEntriesCount = 0;
1017 UINT32 DtboTableEntryOffset = 0;
1018 EFI_STATUS Status = EFI_SUCCESS;
1019 UINT32 DtboImgSz = 0;
1020 CHAR16 PtnName[MAX_GPT_NAME_SIZE] = {0};
1021
1022 /** Get size of user_dtbo partition **/
1023 UINT32 BlkIOAttrib = 0;
1024 PartiSelectFilter HandleFilter;
1025 UINT32 MaxHandles = 1;
1026 EFI_BLOCK_IO_PROTOCOL *BlockIo = NULL;
1027 HandleInfo HandleInfoList[1];
1028
1029 GUARD ( StrnCpyS (PtnName,
1030 MAX_GPT_NAME_SIZE,
1031 (CONST CHAR16 *)L"user_dtbo",
1032 (UINTN)StrLen (L"user_dtbo")));
1033
1034 BlkIOAttrib |= BLK_IO_SEL_PARTITIONED_MBR;
1035 BlkIOAttrib |= BLK_IO_SEL_PARTITIONED_GPT;
1036 BlkIOAttrib |= BLK_IO_SEL_MEDIA_TYPE_NON_REMOVABLE;
1037 BlkIOAttrib |= BLK_IO_SEL_MATCH_PARTITION_LABEL;
1038
1039 HandleFilter.RootDeviceType = NULL;
1040 HandleFilter.PartitionLabel = NULL;
1041 HandleFilter.VolumeName = NULL;
1042 HandleFilter.PartitionLabel = PtnName;
1043
1044 Status =
1045 GetBlkIOHandles (BlkIOAttrib, &HandleFilter, HandleInfoList, &MaxHandles);
1046 if (Status != EFI_SUCCESS ||
1047 MaxHandles != 1) {
1048 DEBUG ((EFI_D_ERROR,
1049 "Override DTB: GetBlkIOHandles failed loading user_dtbo!\n"));
1050 Status = EFI_LOAD_ERROR;
1051 goto err;
1052 }
1053
1054 BlockIo = HandleInfoList[0].BlkIo;
Lijuan Gaobe74b9e2020-09-21 20:29:27 +08001055 DtboImgSz = GetPartitionSize (BlockIo);
1056 if (!DtboImgSz) {
1057 Status = EFI_BAD_BUFFER_SIZE;
1058 goto err;
1059 }
Mayank Grover98dcd672019-02-05 14:46:25 +05301060 *DtboImgBuffer = AllocateZeroPool (DtboImgSz);
1061 if (*DtboImgBuffer == NULL) {
1062 DEBUG ((EFI_D_ERROR, "Override DTB: Buffer allocation failure\n"));
1063 Status = EFI_OUT_OF_RESOURCES;
1064 goto err;
1065 }
1066
1067 /** Load user_dtbo image. **/
1068 Status = LoadImageFromPartition (*DtboImgBuffer, &DtboImgSz, PtnName);
1069 if (Status != EFI_SUCCESS) {
1070 DEBUG ((EFI_D_ERROR, "Override DTB: DtboImgBuffer loading falied\n"));
1071 return Status;
1072 }
1073
1074 DtboTableHdr = (struct DtboTableHdr *)*DtboImgBuffer;
1075 DtboTableEntryOffset = fdt32_to_cpu (DtboTableHdr->DtEntryOffset);
1076 if (CHECK_ADD64 ((UINT64)*DtboImgBuffer, DtboTableEntryOffset)) {
1077 DEBUG ((EFI_D_ERROR,
1078 "Override DTB: Integer overflow detected Dtbo address\n"));
1079 Status = EFI_INVALID_PARAMETER;
1080 goto err;
1081 }
1082
1083 DtboTableEntry =
1084 (struct DtboTableEntry *)(*DtboImgBuffer + DtboTableEntryOffset);
1085 if (!DtboTableEntry) {
1086 DEBUG ((EFI_D_ERROR, "Override DTB: No proper DtTable\n"));
1087 Status = EFI_INVALID_PARAMETER;
1088 goto err;
1089 }
1090
1091 // Support only one dtb in user dtbo image.
1092 DtboTableEntriesCount = fdt32_to_cpu (DtboTableHdr->DtEntryCount);
1093 if (DtboTableEntriesCount > 1) {
1094 DEBUG ((EFI_D_ERROR,
1095 "Override DTB: Exceeding maximum supported dtb count in Image\n"));
1096 Status = EFI_INVALID_PARAMETER;
1097 goto err;
1098 }
1099
1100 OvrdDtb = *DtboImgBuffer + fdt32_to_cpu (DtboTableEntry->DtOffset);
1101 if (fdt_check_header (OvrdDtb) ||
1102 fdt_check_header_ext (OvrdDtb)) {
1103 DEBUG ((EFI_D_ERROR, "Override DTB: No Valid DTB in image\n"));
1104 Status = EFI_INVALID_PARAMETER;
1105 goto err;
1106 }
1107 *DtboImgBuffer = OvrdDtb;
1108 return Status;
1109
1110err:
1111 if (*DtboImgBuffer) {
1112 FreePool (*DtboImgBuffer);
1113 }
1114 return Status;
1115}
1116
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001117VOID *
1118GetBoardDtb (BootInfo *Info, VOID *DtboImgBuffer)
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301119{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001120 struct DtboTableHdr *DtboTableHdr = DtboImgBuffer;
1121 struct DtboTableEntry *DtboTableEntry = NULL;
1122 UINT32 DtboCount = 0;
1123 VOID *BoardDtb = NULL;
1124 UINT32 DtboTableEntriesCount = 0;
1125 UINT32 FirstDtboTableEntryOffset = 0;
1126 DtInfo CurDtbInfo = {0};
1127 DtInfo BestDtbInfo = {0};
lijuang6fa55c02018-06-22 19:19:26 +08001128 BOOLEAN FindBestDtb = FALSE;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301129
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001130 if (!DtboImgBuffer) {
1131 DEBUG ((EFI_D_ERROR, "Dtbo Img buffer is NULL\n"));
1132 return NULL;
1133 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301134
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001135 FirstDtboTableEntryOffset = fdt32_to_cpu (DtboTableHdr->DtEntryOffset);
1136 if (CHECK_ADD64 ((UINT64)DtboImgBuffer, FirstDtboTableEntryOffset)) {
1137 DEBUG ((EFI_D_ERROR, "Integer overflow deteced with Dtbo address\n"));
1138 return NULL;
1139 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301140
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001141 DtboTableEntry =
1142 (struct DtboTableEntry *)(DtboImgBuffer + FirstDtboTableEntryOffset);
1143 if (!DtboTableEntry) {
1144 DEBUG ((EFI_D_ERROR, "No proper DtTable\n"));
1145 return NULL;
1146 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +05301147
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001148 DtboTableEntriesCount = fdt32_to_cpu (DtboTableHdr->DtEntryCount);
1149 for (DtboCount = 0; DtboCount < DtboTableEntriesCount; DtboCount++) {
1150 if (CHECK_ADD64 ((UINT64)DtboImgBuffer,
1151 fdt32_to_cpu (DtboTableEntry->DtOffset))) {
Mukesh Ojha82911f82018-04-30 16:45:07 +05301152 DEBUG ((EFI_D_ERROR, "Integer overflow detected with Dtbo address\n"));
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001153 return NULL;
1154 }
1155 BoardDtb = DtboImgBuffer + fdt32_to_cpu (DtboTableEntry->DtOffset);
1156 if (fdt_check_header (BoardDtb) || fdt_check_header_ext (BoardDtb)) {
1157 DEBUG ((EFI_D_ERROR, "No Valid Dtb\n"));
1158 break;
1159 }
Mukesh Ojhaa1bd1f82017-05-25 19:04:21 +05301160
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001161 CurDtbInfo.Dtb = BoardDtb;
lijuang6fa55c02018-06-22 19:19:26 +08001162 FindBestDtb = ReadDtbFindMatch (&CurDtbInfo, &BestDtbInfo, VARIANT_MATCH);
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001163 DEBUG ((EFI_D_VERBOSE, "Dtbo count = %u LocalBoardDtMatch = %x"
1164 "\n",
1165 DtboCount, CurDtbInfo.DtMatchVal));
lijuang6fa55c02018-06-22 19:19:26 +08001166
1167 if (FindBestDtb) {
1168 DtboIdx = DtboCount;
1169 }
1170
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001171 DtboTableEntry++;
1172 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301173
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001174 if (!BestDtbInfo.Dtb) {
1175 DEBUG ((EFI_D_ERROR, "Unable to find the Board Dtb\n"));
1176 return NULL;
1177 }
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301178
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001179 return BestDtbInfo.Dtb;
Vijay Kumar Pendoti644a20b2017-03-15 18:22:17 +05301180}
1181
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001182/* Returns 0 if the device tree is valid. */
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001183int
1184DeviceTreeValidate (UINT8 *DeviceTreeBuff,
1185 UINT32 PageSize,
1186 UINT32 *DeviceTreeSize)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001187{
lijuangc336a292017-08-22 19:48:49 +08001188 UINT32 dt_entry_size;
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001189 UINT64 hdr_size;
1190 struct dt_table *table;
1191 if (DeviceTreeSize) {
1192 table = (struct dt_table *)DeviceTreeBuff;
1193 if (table->magic != DEV_TREE_MAGIC) {
1194 // bad magic in device tree table
1195 return -1;
1196 }
1197 if (table->version == DEV_TREE_VERSION_V1) {
1198 dt_entry_size = sizeof (struct dt_entry_v1);
1199 } else if (table->version == DEV_TREE_VERSION_V2) {
1200 dt_entry_size = sizeof (struct dt_entry_v2);
1201 } else if (table->version == DEV_TREE_VERSION_V3) {
1202 dt_entry_size = sizeof (struct dt_entry);
1203 } else {
1204 // unsupported dt version
1205 return -1;
1206 }
1207 hdr_size =
1208 ((UINT64)table->num_entries * dt_entry_size) + DEV_TREE_HEADER_SIZE;
1209 // hdr_size = ROUNDUP(hdr_size, PageSize);
1210 hdr_size = EFI_SIZE_TO_PAGES (hdr_size);
1211 if (hdr_size > MAX_UINT64)
1212 return -1;
1213 else
1214 *DeviceTreeSize = hdr_size & MAX_UINT64;
1215 // dt_entry_ptr = (struct dt_entry *)((CHAR8 *)table +
1216 // DEV_TREE_HEADER_SIZE);
1217 // table_ptr = dt_entry_ptr;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001218
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001219 DEBUG ((EFI_D_ERROR, "DT Total number of entries: %d, DTB version: %d\n",
1220 table->num_entries, table->version));
1221 }
1222 return 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001223}
1224
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001225STATIC int
1226platform_dt_absolute_match (struct dt_entry *cur_dt_entry,
1227 struct dt_entry_node *dt_list)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001228{
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +05301229 UINT32 cur_dt_hlos_ddr;
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001230 UINT32 cur_dt_hw_platform;
1231 UINT32 cur_dt_hw_subtype;
1232 UINT32 cur_dt_msm_id;
1233 dt_node *dt_node_tmp = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001234
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001235 /* Platform-id
1236 * bit no |31 24|23 16|15 0|
1237 * |reserved|foundry-id|msm-id|
1238 */
1239 cur_dt_msm_id = (cur_dt_entry->platform_id & 0x0000ffff);
1240 cur_dt_hw_platform = (cur_dt_entry->variant_id & 0x000000ff);
1241 cur_dt_hw_subtype = (cur_dt_entry->board_hw_subtype & 0xff);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001242
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +05301243 /* Bits 10:8 contain ddr information */
1244 cur_dt_hlos_ddr = (cur_dt_entry->board_hw_subtype & 0x700);
1245
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001246 /* 1. must match the msm_id, platform_hw_id, platform_subtype and DDR size
1247 * soc, board major/minor, pmic major/minor must less than board info
1248 * 2. find the matched DTB then return 1
1249 * 3. otherwise return 0
1250 */
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001251
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001252 if ((cur_dt_msm_id == (BoardPlatformRawChipId () & 0x0000ffff)) &&
1253 (cur_dt_hw_platform == BoardPlatformType ()) &&
1254 (cur_dt_hw_subtype == BoardPlatformSubType ()) &&
Chandra Sai Chidipudi64ced632020-01-22 12:25:28 +05301255 (cur_dt_hlos_ddr == (BoardPlatformHlosSubType() & 0x700)) &&
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001256 (cur_dt_entry->soc_rev <= BoardPlatformChipVersion ()) &&
1257 ((cur_dt_entry->variant_id & 0x00ffff00) <=
1258 (BoardTargetId () & 0x00ffff00)) &&
1259 (cur_dt_entry->pmic_rev[0] <= BoardPmicTarget (0)) &&
1260 (cur_dt_entry->pmic_rev[1] <= BoardPmicTarget (1)) &&
1261 (cur_dt_entry->pmic_rev[2] <= BoardPmicTarget (2)) &&
1262 (cur_dt_entry->pmic_rev[3] <= BoardPmicTarget (3))) {
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001263
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001264 dt_node_tmp = dt_entry_list_init ();
1265 if (!dt_node_tmp) {
1266 DEBUG ((EFI_D_ERROR, "dt_node_tmp is NULL\n"));
1267 return 0;
1268 }
Vijay Kumar Pendoti1e847bd2016-11-21 15:29:13 +05301269
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001270 gBS->CopyMem ((VOID *)dt_node_tmp->dt_entry_m, (VOID *)cur_dt_entry,
1271 sizeof (struct dt_entry));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001272
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001273 DEBUG (
1274 (EFI_D_VERBOSE,
1275 "Add DTB entry 0x%x/%08x/0x%08x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
1276 dt_node_tmp->dt_entry_m->platform_id,
1277 dt_node_tmp->dt_entry_m->variant_id,
1278 dt_node_tmp->dt_entry_m->board_hw_subtype,
1279 dt_node_tmp->dt_entry_m->soc_rev, dt_node_tmp->dt_entry_m->pmic_rev[0],
1280 dt_node_tmp->dt_entry_m->pmic_rev[1],
1281 dt_node_tmp->dt_entry_m->pmic_rev[2],
1282 dt_node_tmp->dt_entry_m->pmic_rev[3], dt_node_tmp->dt_entry_m->offset,
1283 dt_node_tmp->dt_entry_m->size));
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001284
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001285 insert_dt_entry_in_queue (dt_list, dt_node_tmp);
1286 return 1;
1287 }
1288 return 0;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001289}
1290
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001291int
1292platform_dt_absolute_compat_match (struct dt_entry_node *dt_list,
1293 UINT32 dtb_info)
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001294{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001295 struct dt_entry_node *dt_node_tmp1 = NULL;
1296 struct dt_entry_node *dt_node_tmp2 = NULL;
1297 UINT32 current_info = 0;
1298 UINT32 board_info = 0;
1299 UINT32 best_info = 0;
1300 UINT32 current_pmic_model[4] = {0, 0, 0, 0};
1301 UINT32 board_pmic_model[4] = {0, 0, 0, 0};
1302 UINT32 best_pmic_model[4] = {0, 0, 0, 0};
1303 UINT32 delete_current_dt = 0;
1304 UINT32 i;
1305
1306 /* start to select the exact entry
1307 * default to exact match 0, if find current DTB entry info is the same as
1308 * board info,
1309 * then exact match board info.
1310 */
1311 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1312 {
1313 if (!dt_node_tmp1) {
1314 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1315 break;
1316 }
1317 switch (dtb_info) {
1318 case DTB_FOUNDRY:
1319 current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
1320 board_info = BoardPlatformFoundryId () << 16;
1321 break;
Chandra Sai Chidipudi3b1450c2020-02-07 15:06:15 +05301322 case DTB_DDR:
1323 current_info = ((dt_node_tmp1->dt_entry_m->board_hw_subtype) & 0x700);
1324 board_info = (BoardPlatformHlosSubType () & 0x700);
1325 break;
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001326 case DTB_PMIC_MODEL:
1327 for (i = 0; i < 4; i++) {
1328 current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
1329 board_pmic_model[i] = BoardPmicModel (i);
1330 }
1331 break;
1332 default:
1333 DEBUG ((EFI_D_ERROR,
1334 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1335 return 0;
1336 }
1337
1338 if (dtb_info == DTB_PMIC_MODEL) {
1339 if ((current_pmic_model[0] == board_pmic_model[0]) &&
1340 (current_pmic_model[1] == board_pmic_model[1]) &&
1341 (current_pmic_model[2] == board_pmic_model[2]) &&
1342 (current_pmic_model[3] == board_pmic_model[3])) {
1343
1344 for (i = 0; i < 4; i++) {
1345 best_pmic_model[i] = current_pmic_model[i];
1346 }
1347 break;
1348 }
1349 } else {
1350 if (current_info == board_info) {
1351 best_info = current_info;
1352 break;
1353 }
1354 }
1355 }
1356
1357 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1358 {
1359 if (!dt_node_tmp1) {
1360 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1361 break;
1362 }
1363 switch (dtb_info) {
1364 case DTB_FOUNDRY:
1365 current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000);
1366 break;
Chandra Sai Chidipudi3b1450c2020-02-07 15:06:15 +05301367 case DTB_DDR:
1368 current_info = ((dt_node_tmp1->dt_entry_m->board_hw_subtype) & 0x700);
1369 break;
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001370 case DTB_PMIC_MODEL:
1371 for (i = 0; i < 4; i++) {
1372 current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff);
1373 }
1374 break;
1375 default:
1376 DEBUG ((EFI_D_ERROR,
1377 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1378 return 0;
1379 }
1380
1381 if (dtb_info == DTB_PMIC_MODEL) {
1382 if ((current_pmic_model[0] != best_pmic_model[0]) ||
1383 (current_pmic_model[1] != best_pmic_model[1]) ||
1384 (current_pmic_model[2] != best_pmic_model[2]) ||
1385 (current_pmic_model[3] != best_pmic_model[3])) {
1386
1387 delete_current_dt = 1;
1388 }
1389 } else {
1390 if (current_info != best_info) {
1391 delete_current_dt = 1;
1392 }
1393 }
1394
1395 if (delete_current_dt) {
1396 DEBUG (
1397 (EFI_D_VERBOSE,
1398 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1399 dt_node_tmp1->dt_entry_m->platform_id,
1400 dt_node_tmp1->dt_entry_m->variant_id,
1401 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1402 dt_node_tmp1->dt_entry_m->soc_rev,
1403 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1404 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1405 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1406 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1407 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1408
1409 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1410 dt_entry_list_delete (dt_node_tmp1);
1411 dt_node_tmp1 = dt_node_tmp2;
1412 delete_current_dt = 0;
1413 }
1414 }
1415
1416 return 1;
1417}
1418
1419int
1420update_dtb_entry_node (struct dt_entry_node *dt_list, UINT32 dtb_info)
1421{
1422 struct dt_entry_node *dt_node_tmp1 = NULL;
1423 struct dt_entry_node *dt_node_tmp2 = NULL;
1424 UINT32 current_info = 0;
1425 UINT32 board_info = 0;
1426 UINT32 best_info = 0;
1427
1428 /* start to select the best entry*/
1429 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1430 {
1431 if (!dt_node_tmp1) {
1432 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1433 break;
1434 }
1435 switch (dtb_info) {
1436 case DTB_SOC:
1437 current_info = dt_node_tmp1->dt_entry_m->soc_rev;
1438 board_info = BoardPlatformChipVersion ();
1439 break;
1440 case DTB_MAJOR_MINOR:
1441 current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
1442 board_info = BoardTargetId () & 0x00ffff00;
1443 break;
1444 case DTB_PMIC0:
1445 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0];
1446 board_info = BoardPmicTarget (0);
1447 break;
1448 case DTB_PMIC1:
1449 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1];
1450 board_info = BoardPmicTarget (1);
1451 break;
1452 case DTB_PMIC2:
1453 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2];
1454 board_info = BoardPmicTarget (2);
1455 break;
1456 case DTB_PMIC3:
1457 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3];
1458 board_info = BoardPmicTarget (3);
1459 break;
1460 default:
1461 DEBUG ((EFI_D_ERROR,
1462 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1463 return 0;
1464 }
1465
1466 if (current_info == board_info) {
1467 best_info = current_info;
1468 break;
1469 }
1470 if ((current_info < board_info) && (current_info > best_info)) {
1471 best_info = current_info;
1472 }
1473 if (current_info < best_info) {
1474 DEBUG (
1475 (EFI_D_ERROR,
1476 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1477 dt_node_tmp1->dt_entry_m->platform_id,
1478 dt_node_tmp1->dt_entry_m->variant_id,
1479 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1480 dt_node_tmp1->dt_entry_m->soc_rev,
1481 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1482 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1483 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1484 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1485 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1486
1487 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1488 dt_entry_list_delete (dt_node_tmp1);
1489 dt_node_tmp1 = dt_node_tmp2;
1490 }
1491 }
1492
1493 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1494 {
1495 if (!dt_node_tmp1) {
1496 DEBUG ((EFI_D_ERROR, "Current node is the end\n"));
1497 break;
1498 }
1499 switch (dtb_info) {
1500 case DTB_SOC:
1501 current_info = dt_node_tmp1->dt_entry_m->soc_rev;
1502 break;
1503 case DTB_MAJOR_MINOR:
1504 current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00);
1505 break;
1506 case DTB_PMIC0:
1507 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[0];
1508 break;
1509 case DTB_PMIC1:
1510 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[1];
1511 break;
1512 case DTB_PMIC2:
1513 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[2];
1514 break;
1515 case DTB_PMIC3:
1516 current_info = dt_node_tmp1->dt_entry_m->pmic_rev[3];
1517 break;
1518 default:
1519 DEBUG ((EFI_D_ERROR,
1520 "ERROR: Unsupported version (%d) in dt node check \n", dtb_info));
1521 return 0;
1522 }
1523
1524 if (current_info != best_info) {
1525 DEBUG (
1526 (EFI_D_VERBOSE,
1527 "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
1528 dt_node_tmp1->dt_entry_m->platform_id,
1529 dt_node_tmp1->dt_entry_m->variant_id,
1530 dt_node_tmp1->dt_entry_m->board_hw_subtype,
1531 dt_node_tmp1->dt_entry_m->soc_rev,
1532 dt_node_tmp1->dt_entry_m->pmic_rev[0],
1533 dt_node_tmp1->dt_entry_m->pmic_rev[1],
1534 dt_node_tmp1->dt_entry_m->pmic_rev[2],
1535 dt_node_tmp1->dt_entry_m->pmic_rev[3],
1536 dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size));
1537
1538 dt_node_tmp2 = (struct dt_entry_node *)dt_node_tmp1->node.prev;
1539 dt_entry_list_delete (dt_node_tmp1);
1540 dt_node_tmp1 = dt_node_tmp2;
1541 }
1542 }
1543 return 1;
1544}
1545
1546STATIC struct dt_entry *
1547platform_dt_match_best (struct dt_entry_node *dt_list)
1548{
1549 struct dt_entry_node *dt_node_tmp1 = NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001550
lijuangc336a292017-08-22 19:48:49 +08001551 /* check Foundry id
1552 * the foundry id must exact match board founddry id, this is compatibility
1553 * check, if couldn't find the exact match from DTB, will exact match 0x0.
1554 */
1555 platform_dt_absolute_compat_match (dt_list, DTB_FOUNDRY);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001556
Chandra Sai Chidipudi3b1450c2020-02-07 15:06:15 +05301557 /* check DDR type
1558 * the DDR type must exact match board DDR tpe, this is compatibility
1559 * check, if couldn't find the exact match from DTB, will exact match 0x0.
1560 */
1561 platform_dt_absolute_compat_match (dt_list, DTB_DDR);
1562
lijuangc336a292017-08-22 19:48:49 +08001563 /* check PMIC model
1564 * the PMIC model must exact match board PMIC model, this is compatibility
1565 * check, if couldn't find the exact match from DTB, will exact match 0x0.
1566 */
1567 platform_dt_absolute_compat_match (dt_list, DTB_PMIC_MODEL);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001568
lijuangc336a292017-08-22 19:48:49 +08001569 /* check soc version
1570 * the suitable soc version must less than or equal to board soc version
1571 */
1572 update_dtb_entry_node (dt_list, DTB_SOC);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001573
lijuangc336a292017-08-22 19:48:49 +08001574 /*check major and minor version
1575 * the suitable major&minor version must less than or equal to board
1576 * major&minor version
1577 */
1578 update_dtb_entry_node (dt_list, DTB_MAJOR_MINOR);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001579
lijuangc336a292017-08-22 19:48:49 +08001580 /*check pmic info
1581 * the suitable pmic major&minor info must less than or equal to board pmic
1582 * major&minor version
1583 */
1584 update_dtb_entry_node (dt_list, DTB_PMIC0);
1585 update_dtb_entry_node (dt_list, DTB_PMIC1);
1586 update_dtb_entry_node (dt_list, DTB_PMIC2);
1587 update_dtb_entry_node (dt_list, DTB_PMIC3);
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001588
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001589 list_for_every_entry (&dt_list->node, dt_node_tmp1, dt_node, node)
1590 {
1591 if (!dt_node_tmp1) {
1592 DEBUG ((EFI_D_ERROR, "ERROR: Couldn't find the suitable DTB!\n"));
1593 return NULL;
1594 }
1595 if (dt_node_tmp1->dt_entry_m)
1596 return dt_node_tmp1->dt_entry_m;
1597 }
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001598
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001599 return NULL;
Sridhar Parasuramc8f50022015-12-05 10:36:04 -08001600}
Bhanuprakash Modemed797c72018-09-05 20:39:07 +05301601
1602BOOLEAN
1603AppendToDtList (struct fdt_entry_node **DtList,
1604 UINT64 Address,
1605 UINT64 Size) {
1606 struct fdt_entry_node *Current = *DtList;
1607
1608 if (*DtList == NULL) {
1609 DEBUG ((EFI_D_VERBOSE, "DTs list: NULL\n"));
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +05301610 Current = AllocateZeroPool (sizeof (struct fdt_entry_node));
Bhanuprakash Modemed797c72018-09-05 20:39:07 +05301611 if (!Current) {
1612 return FALSE;
1613 }
1614
1615 Current->address = Address;
1616 Current->size = Size;
1617 Current->next = NULL;
1618 *DtList = Current;
1619 return TRUE;
1620 } else {
1621
1622 while (Current->next != NULL) {
1623 Current = Current->next;
1624 }
1625
Bhanuprakash Modem763cdd52018-11-01 14:49:55 +05301626 Current->next = AllocateZeroPool (sizeof (struct fdt_entry_node));
Bhanuprakash Modemed797c72018-09-05 20:39:07 +05301627 if (!Current->next) {
1628 return FALSE;
1629 }
1630 Current->next->address = Address;
1631 Current->next->size = Size;
1632 Current->next->next = NULL;
1633 return TRUE;
1634 }
1635}
1636
1637VOID DeleteDtList (struct fdt_entry_node** DtList)
1638{
1639 struct fdt_entry_node *Current = *DtList;
1640 struct fdt_entry_node *Next;
1641
1642 while (Current != NULL) {
1643 Next = Current->next;
1644 FreePool (Current);
1645 Current = Next;
1646 }
1647
1648 *DtList = NULL;
1649}