blob: c4f009affc5e84335f3290a3b4e0f67e7431e0c6 [file] [log] [blame]
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2019 - 2020 Xilinx, Inc.
4 */
5
6#include <common.h>
7#include <cpu_func.h>
8#include <env.h>
9#include <fdtdec.h>
10#include <log.h>
11#include <malloc.h>
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +010012#include <net.h>
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053013#include <asm/io.h>
14#include <asm/arch/hardware.h>
15
16#include "fru.h"
17
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020018struct fru_table fru_data __section(".data");
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053019
20static u16 fru_cal_area_len(u8 len)
21{
22 return len * FRU_COMMON_HDR_LEN_MULTIPLIER;
23}
24
25static u8 fru_version(u8 ver)
26{
27 return ver & FRU_COMMON_HDR_VER_MASK;
28}
29
30static int fru_check_language(u8 code)
31{
32 if (code != FRU_LANG_CODE_ENGLISH && code != FRU_LANG_CODE_ENGLISH_1) {
33 printf("FRU_ERROR: Only English Language is supported\n");
34 return -EINVAL;
35 }
36
37 return 0;
38}
39
40u8 fru_checksum(u8 *addr, u8 len)
41{
42 u8 checksum = 0;
Ashok Reddy Soma6f049992022-02-23 15:00:57 +010043 u8 cnt = len;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053044
45 while (len--) {
Ashok Reddy Soma6f049992022-02-23 15:00:57 +010046 if (*addr == 0)
47 cnt--;
48
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053049 checksum += *addr;
50 addr++;
51 }
52
Ashok Reddy Soma6f049992022-02-23 15:00:57 +010053 /* If all data bytes are 0's return error */
54 if (!cnt)
55 return EINVAL;
56
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053057 return checksum;
58}
59
60static int fru_check_type_len(u8 type_len, u8 language, u8 *type)
61{
62 int len;
63
64 if (type_len == FRU_TYPELEN_EOF)
65 return -EINVAL;
66
67 *type = (type_len & FRU_TYPELEN_CODE_MASK) >> FRU_TYPELEN_TYPE_SHIFT;
68
69 len = type_len & FRU_TYPELEN_LEN_MASK;
70
71 return len;
72}
73
Michal Simekd5c33212019-04-15 13:54:09 +020074/* Return len */
75static u8 fru_gen_type_len(u8 *addr, char *name)
76{
77 int len = strlen(name);
78 struct fru_board_info_member *member;
79
80 member = (struct fru_board_info_member *)addr;
81 member->type_len = FRU_TYPELEN_TYPE_ASCII8 << FRU_TYPELEN_TYPE_SHIFT;
82 member->type_len |= len;
83
84 debug("%lx/%lx: Add %s to 0x%lx (len 0x%x)\n", (ulong)addr,
85 (ulong)&member->type_len, name, (ulong)&member->name, len);
86 memcpy(&member->name, name, len);
87
88 /* Add +1 for type_len parameter */
89 return 1 + len;
90}
91
92int fru_generate(unsigned long addr, char *manufacturer, char *board_name,
93 char *serial_no, char *part_no, char *revision)
94{
95 struct fru_common_hdr *header = (struct fru_common_hdr *)addr;
96 struct fru_board_info_header *board_info;
97 u8 *member;
98 u8 len, pad, modulo;
99
100 header->version = 1; /* Only version 1.0 is supported now */
101 header->off_internal = 0; /* not present */
102 header->off_chassis = 0; /* not present */
103 header->off_board = (sizeof(*header)) / 8; /* Starting offset 8 */
104 header->off_product = 0; /* not present */
105 header->off_multirec = 0; /* not present */
106 header->pad = 0;
107 /*
108 * This unsigned byte can be used to calculate a zero checksum
109 * for the data area following the header. I.e. the modulo 256 sum of
110 * the record data bytes plus the checksum byte equals zero.
111 */
112 header->crc = 0; /* Clear before calculation */
113 header->crc = 0 - fru_checksum((u8 *)header, sizeof(*header));
114
115 /* board info is just right after header */
116 board_info = (void *)((u8 *)header + sizeof(*header));
117
118 debug("header %lx, board_info %lx\n", (ulong)header, (ulong)board_info);
119
120 board_info->ver = 1; /* 1.0 spec */
121 board_info->lang_code = 0; /* English */
122 board_info->time[0] = 0; /* unspecified */
123 board_info->time[1] = 0; /* unspecified */
124 board_info->time[2] = 0; /* unspecified */
125
126 /* Member fields are just after board_info header */
127 member = (u8 *)board_info + sizeof(*board_info);
128
129 len = fru_gen_type_len(member, manufacturer); /* Board Manufacturer */
130 member += len;
131 len = fru_gen_type_len(member, board_name); /* Board Product name */
132 member += len;
133 len = fru_gen_type_len(member, serial_no); /* Board Serial number */
134 member += len;
135 len = fru_gen_type_len(member, part_no); /* Board part number */
136 member += len;
137 len = fru_gen_type_len(member, "U-Boot generator"); /* File ID */
138 member += len;
139 len = fru_gen_type_len(member, revision); /* Revision */
140 member += len;
141
142 *member++ = 0xc1; /* Indication of no more fields */
143
144 len = member - (u8 *)board_info; /* Find current length */
145 len += 1; /* Add checksum there too for calculation */
146
147 modulo = len % 8;
148
149 if (modulo) {
150 /* Do not fill last item which is checksum */
151 for (pad = 0; pad < 8 - modulo; pad++)
152 *member++ = 0;
153
154 /* Increase structure size */
155 len += 8 - modulo;
156 }
157
158 board_info->len = len / 8; /* Size in multiples of 8 bytes */
159
160 *member = 0; /* Clear before calculation */
161 *member = 0 - fru_checksum((u8 *)board_info, len);
162
163 debug("checksum %x(addr %x)\n", *member, len);
164
165 env_set_hex("fru_addr", addr);
166 env_set_hex("filesize", (unsigned long)member - addr + 1);
167
168 return 0;
169}
170
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530171static int fru_parse_board(unsigned long addr)
172{
173 u8 i, type;
174 int len;
Michal Simekbd8d50f2020-11-06 13:55:45 +0100175 u8 *data, *term, *limit;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530176
177 memcpy(&fru_data.brd.ver, (void *)addr, 6);
178 addr += 6;
179 data = (u8 *)&fru_data.brd.manufacturer_type_len;
180
Michal Simekbd8d50f2020-11-06 13:55:45 +0100181 /* Record max structure limit not to write data over allocated space */
Heinrich Schuchardt575d4072021-01-03 18:07:53 +0100182 limit = (u8 *)&fru_data.brd + sizeof(struct fru_board_data);
Michal Simekbd8d50f2020-11-06 13:55:45 +0100183
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530184 for (i = 0; ; i++, data += FRU_BOARD_MAX_LEN) {
185 len = fru_check_type_len(*(u8 *)addr, fru_data.brd.lang_code,
186 &type);
187 /*
188 * Stop cature if it end of fields
189 */
190 if (len == -EINVAL)
191 break;
192
Michal Simekbd8d50f2020-11-06 13:55:45 +0100193 /* Stop when amount of chars is more then fields to record */
194 if (data + len > limit)
195 break;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530196 /* This record type/len field */
197 *data++ = *(u8 *)addr;
198
199 /* Add offset to match data */
200 addr += 1;
201
202 /* If len is 0 it means empty field that's why skip writing */
203 if (!len)
204 continue;
205
206 /* Record data field */
207 memcpy(data, (u8 *)addr, len);
208 term = data + (u8)len;
209 *term = 0;
210 addr += len;
211 }
212
213 if (i < FRU_BOARD_AREA_TOTAL_FIELDS) {
214 printf("Board area require minimum %d fields\n",
215 FRU_BOARD_AREA_TOTAL_FIELDS);
216 return -EINVAL;
217 }
218
219 return 0;
220}
221
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100222static int fru_parse_multirec(unsigned long addr)
223{
224 struct fru_multirec_hdr mrc;
225 u8 checksum = 0;
226 u8 hdr_len = sizeof(struct fru_multirec_hdr);
227 int mac_len = 0;
228
229 debug("%s: multirec addr %lx\n", __func__, addr);
230
231 do {
232 memcpy(&mrc.rec_type, (void *)addr, hdr_len);
233
234 checksum = fru_checksum((u8 *)addr, hdr_len);
235 if (checksum) {
236 debug("%s header CRC error\n", __func__);
237 return -EINVAL;
238 }
239
240 if (mrc.rec_type == FRU_MULTIREC_TYPE_OEM) {
241 struct fru_multirec_mac *mac = (void *)addr + hdr_len;
Michal Simek292b9ae2022-11-23 12:48:44 +0100242 u32 type = FRU_DUT_MACID;
243
244 if (CONFIG_IS_ENABLED(FRU_SC))
245 type = FRU_SC_MACID;
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100246
Michal Simek292b9ae2022-11-23 12:48:44 +0100247 if (mac->ver == type) {
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100248 mac_len = mrc.len - FRU_MULTIREC_MAC_OFFSET;
249 memcpy(&fru_data.mac.macid, mac->macid, mac_len);
250 }
251 }
252 addr += mrc.len + hdr_len;
253 } while (!(mrc.type & FRU_LAST_REC));
254
255 return 0;
256}
257
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530258int fru_capture(unsigned long addr)
259{
260 struct fru_common_hdr *hdr;
261 u8 checksum = 0;
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100262 unsigned long multirec_addr = addr;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530263
264 checksum = fru_checksum((u8 *)addr, sizeof(struct fru_common_hdr));
265 if (checksum) {
266 printf("%s Common header CRC error\n", __func__);
267 return -EINVAL;
268 }
269
270 hdr = (struct fru_common_hdr *)addr;
Ashok Reddy Soma5cb4d032022-02-23 15:00:56 +0100271 memset((void *)&fru_data, 0, sizeof(fru_data));
Michal Simekb682f002020-11-06 13:53:01 +0100272 memcpy((void *)&fru_data, (void *)hdr,
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530273 sizeof(struct fru_common_hdr));
274
275 fru_data.captured = true;
276
277 if (hdr->off_board) {
278 addr += fru_cal_area_len(hdr->off_board);
279 fru_parse_board(addr);
280 }
281
282 env_set_hex("fru_addr", addr);
283
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100284 if (hdr->off_multirec) {
285 multirec_addr += fru_cal_area_len(hdr->off_multirec);
286 fru_parse_multirec(multirec_addr);
287 }
288
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530289 return 0;
290}
291
292static int fru_display_board(struct fru_board_data *brd, int verbose)
293{
294 u32 time = 0;
295 u8 type;
296 int len;
297 u8 *data;
298 static const char * const typecode[] = {
299 "Binary/Unspecified",
300 "BCD plus",
301 "6-bit ASCII",
302 "8-bit ASCII",
303 "2-byte UNICODE"
304 };
305 static const char * const boardinfo[] = {
306 "Manufacturer Name",
307 "Product Name",
308 "Serial No",
309 "Part Number",
Michal Simekd5c33212019-04-15 13:54:09 +0200310 "File ID",
311 /* Xilinx spec */
312 "Revision Number",
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530313 };
314
315 if (verbose) {
316 printf("*****BOARD INFO*****\n");
317 printf("Version:%d\n", fru_version(brd->ver));
318 printf("Board Area Length:%d\n", fru_cal_area_len(brd->len));
319 }
320
321 if (fru_check_language(brd->lang_code))
322 return -EINVAL;
323
324 time = brd->time[2] << 16 | brd->time[1] << 8 |
325 brd->time[0];
326
327 if (verbose)
328 printf("Time in Minutes from 0:00hrs 1/1/96: %d\n", time);
329
330 data = (u8 *)&brd->manufacturer_type_len;
331
332 for (u8 i = 0; i < (sizeof(boardinfo) / sizeof(*boardinfo)); i++) {
333 len = fru_check_type_len(*data++, brd->lang_code,
334 &type);
335 if (len == -EINVAL) {
336 printf("**** EOF for Board Area ****\n");
337 break;
338 }
339
340 if (type <= FRU_TYPELEN_TYPE_ASCII8 &&
341 (brd->lang_code == FRU_LANG_CODE_ENGLISH ||
342 brd->lang_code == FRU_LANG_CODE_ENGLISH_1))
343 debug("Type code: %s\n", typecode[type]);
344 else
345 debug("Type code: %s\n", typecode[type + 1]);
346
347 if (!len) {
348 debug("%s not found\n", boardinfo[i]);
349 continue;
350 }
351
352 switch (type) {
353 case FRU_TYPELEN_TYPE_BINARY:
354 debug("Length: %d\n", len);
355 printf(" %s: 0x%x\n", boardinfo[i], *data);
356 break;
357 case FRU_TYPELEN_TYPE_ASCII8:
358 debug("Length: %d\n", len);
359 printf(" %s: %s\n", boardinfo[i], data);
360 break;
361 default:
362 debug("Unsupported type %x\n", type);
363 }
364
365 data += FRU_BOARD_MAX_LEN;
366 }
367
368 return 0;
369}
370
371static void fru_display_common_hdr(struct fru_common_hdr *hdr, int verbose)
372{
373 if (!verbose)
374 return;
375
376 printf("*****COMMON HEADER*****\n");
377 printf("Version:%d\n", fru_version(hdr->version));
378 if (hdr->off_internal)
379 printf("Internal Use Area Offset:%d\n",
380 fru_cal_area_len(hdr->off_internal));
381 else
382 printf("*** No Internal Area ***\n");
383
384 if (hdr->off_chassis)
385 printf("Chassis Info Area Offset:%d\n",
386 fru_cal_area_len(hdr->off_chassis));
387 else
388 printf("*** No Chassis Info Area ***\n");
389
390 if (hdr->off_board)
391 printf("Board Area Offset:%d\n",
392 fru_cal_area_len(hdr->off_board));
393 else
394 printf("*** No Board Area ***\n");
395
396 if (hdr->off_product)
397 printf("Product Info Area Offset:%d\n",
398 fru_cal_area_len(hdr->off_product));
399 else
400 printf("*** No Product Info Area ***\n");
401
402 if (hdr->off_multirec)
403 printf("MultiRecord Area Offset:%d\n",
404 fru_cal_area_len(hdr->off_multirec));
405 else
406 printf("*** No MultiRecord Area ***\n");
407}
408
409int fru_display(int verbose)
410{
411 if (!fru_data.captured) {
412 printf("FRU data not available please run fru parse\n");
413 return -EINVAL;
414 }
415
416 fru_display_common_hdr(&fru_data.hdr, verbose);
417
418 return fru_display_board(&fru_data.brd, verbose);
419}