blob: 49846ae3d660c213926b3ce2660be6e9b9c87cb6 [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;
242
243 if (mac->ver == FRU_DUT_MACID) {
244 mac_len = mrc.len - FRU_MULTIREC_MAC_OFFSET;
245 memcpy(&fru_data.mac.macid, mac->macid, mac_len);
246 }
247 }
248 addr += mrc.len + hdr_len;
249 } while (!(mrc.type & FRU_LAST_REC));
250
251 return 0;
252}
253
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530254int fru_capture(unsigned long addr)
255{
256 struct fru_common_hdr *hdr;
257 u8 checksum = 0;
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100258 unsigned long multirec_addr = addr;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530259
260 checksum = fru_checksum((u8 *)addr, sizeof(struct fru_common_hdr));
261 if (checksum) {
262 printf("%s Common header CRC error\n", __func__);
263 return -EINVAL;
264 }
265
266 hdr = (struct fru_common_hdr *)addr;
Ashok Reddy Soma5cb4d032022-02-23 15:00:56 +0100267 memset((void *)&fru_data, 0, sizeof(fru_data));
Michal Simekb682f002020-11-06 13:53:01 +0100268 memcpy((void *)&fru_data, (void *)hdr,
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530269 sizeof(struct fru_common_hdr));
270
271 fru_data.captured = true;
272
273 if (hdr->off_board) {
274 addr += fru_cal_area_len(hdr->off_board);
275 fru_parse_board(addr);
276 }
277
278 env_set_hex("fru_addr", addr);
279
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100280 if (hdr->off_multirec) {
281 multirec_addr += fru_cal_area_len(hdr->off_multirec);
282 fru_parse_multirec(multirec_addr);
283 }
284
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530285 return 0;
286}
287
288static int fru_display_board(struct fru_board_data *brd, int verbose)
289{
290 u32 time = 0;
291 u8 type;
292 int len;
293 u8 *data;
294 static const char * const typecode[] = {
295 "Binary/Unspecified",
296 "BCD plus",
297 "6-bit ASCII",
298 "8-bit ASCII",
299 "2-byte UNICODE"
300 };
301 static const char * const boardinfo[] = {
302 "Manufacturer Name",
303 "Product Name",
304 "Serial No",
305 "Part Number",
Michal Simekd5c33212019-04-15 13:54:09 +0200306 "File ID",
307 /* Xilinx spec */
308 "Revision Number",
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530309 };
310
311 if (verbose) {
312 printf("*****BOARD INFO*****\n");
313 printf("Version:%d\n", fru_version(brd->ver));
314 printf("Board Area Length:%d\n", fru_cal_area_len(brd->len));
315 }
316
317 if (fru_check_language(brd->lang_code))
318 return -EINVAL;
319
320 time = brd->time[2] << 16 | brd->time[1] << 8 |
321 brd->time[0];
322
323 if (verbose)
324 printf("Time in Minutes from 0:00hrs 1/1/96: %d\n", time);
325
326 data = (u8 *)&brd->manufacturer_type_len;
327
328 for (u8 i = 0; i < (sizeof(boardinfo) / sizeof(*boardinfo)); i++) {
329 len = fru_check_type_len(*data++, brd->lang_code,
330 &type);
331 if (len == -EINVAL) {
332 printf("**** EOF for Board Area ****\n");
333 break;
334 }
335
336 if (type <= FRU_TYPELEN_TYPE_ASCII8 &&
337 (brd->lang_code == FRU_LANG_CODE_ENGLISH ||
338 brd->lang_code == FRU_LANG_CODE_ENGLISH_1))
339 debug("Type code: %s\n", typecode[type]);
340 else
341 debug("Type code: %s\n", typecode[type + 1]);
342
343 if (!len) {
344 debug("%s not found\n", boardinfo[i]);
345 continue;
346 }
347
348 switch (type) {
349 case FRU_TYPELEN_TYPE_BINARY:
350 debug("Length: %d\n", len);
351 printf(" %s: 0x%x\n", boardinfo[i], *data);
352 break;
353 case FRU_TYPELEN_TYPE_ASCII8:
354 debug("Length: %d\n", len);
355 printf(" %s: %s\n", boardinfo[i], data);
356 break;
357 default:
358 debug("Unsupported type %x\n", type);
359 }
360
361 data += FRU_BOARD_MAX_LEN;
362 }
363
364 return 0;
365}
366
367static void fru_display_common_hdr(struct fru_common_hdr *hdr, int verbose)
368{
369 if (!verbose)
370 return;
371
372 printf("*****COMMON HEADER*****\n");
373 printf("Version:%d\n", fru_version(hdr->version));
374 if (hdr->off_internal)
375 printf("Internal Use Area Offset:%d\n",
376 fru_cal_area_len(hdr->off_internal));
377 else
378 printf("*** No Internal Area ***\n");
379
380 if (hdr->off_chassis)
381 printf("Chassis Info Area Offset:%d\n",
382 fru_cal_area_len(hdr->off_chassis));
383 else
384 printf("*** No Chassis Info Area ***\n");
385
386 if (hdr->off_board)
387 printf("Board Area Offset:%d\n",
388 fru_cal_area_len(hdr->off_board));
389 else
390 printf("*** No Board Area ***\n");
391
392 if (hdr->off_product)
393 printf("Product Info Area Offset:%d\n",
394 fru_cal_area_len(hdr->off_product));
395 else
396 printf("*** No Product Info Area ***\n");
397
398 if (hdr->off_multirec)
399 printf("MultiRecord Area Offset:%d\n",
400 fru_cal_area_len(hdr->off_multirec));
401 else
402 printf("*** No MultiRecord Area ***\n");
403}
404
405int fru_display(int verbose)
406{
407 if (!fru_data.captured) {
408 printf("FRU data not available please run fru parse\n");
409 return -EINVAL;
410 }
411
412 fru_display_common_hdr(&fru_data.hdr, verbose);
413
414 return fru_display_board(&fru_data.brd, verbose);
415}