blob: 058e750c442f50c347d4abadb0a3fd5a227b3912 [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>
12#include <asm/io.h>
13#include <asm/arch/hardware.h>
14
15#include "fru.h"
16
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020017struct fru_table fru_data __section(".data");
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053018
19static u16 fru_cal_area_len(u8 len)
20{
21 return len * FRU_COMMON_HDR_LEN_MULTIPLIER;
22}
23
24static u8 fru_version(u8 ver)
25{
26 return ver & FRU_COMMON_HDR_VER_MASK;
27}
28
29static int fru_check_language(u8 code)
30{
31 if (code != FRU_LANG_CODE_ENGLISH && code != FRU_LANG_CODE_ENGLISH_1) {
32 printf("FRU_ERROR: Only English Language is supported\n");
33 return -EINVAL;
34 }
35
36 return 0;
37}
38
39u8 fru_checksum(u8 *addr, u8 len)
40{
41 u8 checksum = 0;
Ashok Reddy Soma6f049992022-02-23 15:00:57 +010042 u8 cnt = len;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053043
44 while (len--) {
Ashok Reddy Soma6f049992022-02-23 15:00:57 +010045 if (*addr == 0)
46 cnt--;
47
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053048 checksum += *addr;
49 addr++;
50 }
51
Ashok Reddy Soma6f049992022-02-23 15:00:57 +010052 /* If all data bytes are 0's return error */
53 if (!cnt)
54 return EINVAL;
55
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +053056 return checksum;
57}
58
59static int fru_check_type_len(u8 type_len, u8 language, u8 *type)
60{
61 int len;
62
63 if (type_len == FRU_TYPELEN_EOF)
64 return -EINVAL;
65
66 *type = (type_len & FRU_TYPELEN_CODE_MASK) >> FRU_TYPELEN_TYPE_SHIFT;
67
68 len = type_len & FRU_TYPELEN_LEN_MASK;
69
70 return len;
71}
72
Michal Simekd5c33212019-04-15 13:54:09 +020073/* Return len */
74static u8 fru_gen_type_len(u8 *addr, char *name)
75{
76 int len = strlen(name);
77 struct fru_board_info_member *member;
78
79 member = (struct fru_board_info_member *)addr;
80 member->type_len = FRU_TYPELEN_TYPE_ASCII8 << FRU_TYPELEN_TYPE_SHIFT;
81 member->type_len |= len;
82
83 debug("%lx/%lx: Add %s to 0x%lx (len 0x%x)\n", (ulong)addr,
84 (ulong)&member->type_len, name, (ulong)&member->name, len);
85 memcpy(&member->name, name, len);
86
87 /* Add +1 for type_len parameter */
88 return 1 + len;
89}
90
91int fru_generate(unsigned long addr, char *manufacturer, char *board_name,
92 char *serial_no, char *part_no, char *revision)
93{
94 struct fru_common_hdr *header = (struct fru_common_hdr *)addr;
95 struct fru_board_info_header *board_info;
96 u8 *member;
97 u8 len, pad, modulo;
98
99 header->version = 1; /* Only version 1.0 is supported now */
100 header->off_internal = 0; /* not present */
101 header->off_chassis = 0; /* not present */
102 header->off_board = (sizeof(*header)) / 8; /* Starting offset 8 */
103 header->off_product = 0; /* not present */
104 header->off_multirec = 0; /* not present */
105 header->pad = 0;
106 /*
107 * This unsigned byte can be used to calculate a zero checksum
108 * for the data area following the header. I.e. the modulo 256 sum of
109 * the record data bytes plus the checksum byte equals zero.
110 */
111 header->crc = 0; /* Clear before calculation */
112 header->crc = 0 - fru_checksum((u8 *)header, sizeof(*header));
113
114 /* board info is just right after header */
115 board_info = (void *)((u8 *)header + sizeof(*header));
116
117 debug("header %lx, board_info %lx\n", (ulong)header, (ulong)board_info);
118
119 board_info->ver = 1; /* 1.0 spec */
120 board_info->lang_code = 0; /* English */
121 board_info->time[0] = 0; /* unspecified */
122 board_info->time[1] = 0; /* unspecified */
123 board_info->time[2] = 0; /* unspecified */
124
125 /* Member fields are just after board_info header */
126 member = (u8 *)board_info + sizeof(*board_info);
127
128 len = fru_gen_type_len(member, manufacturer); /* Board Manufacturer */
129 member += len;
130 len = fru_gen_type_len(member, board_name); /* Board Product name */
131 member += len;
132 len = fru_gen_type_len(member, serial_no); /* Board Serial number */
133 member += len;
134 len = fru_gen_type_len(member, part_no); /* Board part number */
135 member += len;
136 len = fru_gen_type_len(member, "U-Boot generator"); /* File ID */
137 member += len;
138 len = fru_gen_type_len(member, revision); /* Revision */
139 member += len;
140
141 *member++ = 0xc1; /* Indication of no more fields */
142
143 len = member - (u8 *)board_info; /* Find current length */
144 len += 1; /* Add checksum there too for calculation */
145
146 modulo = len % 8;
147
148 if (modulo) {
149 /* Do not fill last item which is checksum */
150 for (pad = 0; pad < 8 - modulo; pad++)
151 *member++ = 0;
152
153 /* Increase structure size */
154 len += 8 - modulo;
155 }
156
157 board_info->len = len / 8; /* Size in multiples of 8 bytes */
158
159 *member = 0; /* Clear before calculation */
160 *member = 0 - fru_checksum((u8 *)board_info, len);
161
162 debug("checksum %x(addr %x)\n", *member, len);
163
164 env_set_hex("fru_addr", addr);
165 env_set_hex("filesize", (unsigned long)member - addr + 1);
166
167 return 0;
168}
169
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530170static int fru_parse_board(unsigned long addr)
171{
172 u8 i, type;
173 int len;
Michal Simekbd8d50f2020-11-06 13:55:45 +0100174 u8 *data, *term, *limit;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530175
176 memcpy(&fru_data.brd.ver, (void *)addr, 6);
177 addr += 6;
178 data = (u8 *)&fru_data.brd.manufacturer_type_len;
179
Michal Simekbd8d50f2020-11-06 13:55:45 +0100180 /* Record max structure limit not to write data over allocated space */
Heinrich Schuchardt575d4072021-01-03 18:07:53 +0100181 limit = (u8 *)&fru_data.brd + sizeof(struct fru_board_data);
Michal Simekbd8d50f2020-11-06 13:55:45 +0100182
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530183 for (i = 0; ; i++, data += FRU_BOARD_MAX_LEN) {
184 len = fru_check_type_len(*(u8 *)addr, fru_data.brd.lang_code,
185 &type);
186 /*
187 * Stop cature if it end of fields
188 */
189 if (len == -EINVAL)
190 break;
191
Michal Simekbd8d50f2020-11-06 13:55:45 +0100192 /* Stop when amount of chars is more then fields to record */
193 if (data + len > limit)
194 break;
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530195 /* This record type/len field */
196 *data++ = *(u8 *)addr;
197
198 /* Add offset to match data */
199 addr += 1;
200
201 /* If len is 0 it means empty field that's why skip writing */
202 if (!len)
203 continue;
204
205 /* Record data field */
206 memcpy(data, (u8 *)addr, len);
207 term = data + (u8)len;
208 *term = 0;
209 addr += len;
210 }
211
212 if (i < FRU_BOARD_AREA_TOTAL_FIELDS) {
213 printf("Board area require minimum %d fields\n",
214 FRU_BOARD_AREA_TOTAL_FIELDS);
215 return -EINVAL;
216 }
217
218 return 0;
219}
220
221int fru_capture(unsigned long addr)
222{
223 struct fru_common_hdr *hdr;
224 u8 checksum = 0;
225
226 checksum = fru_checksum((u8 *)addr, sizeof(struct fru_common_hdr));
227 if (checksum) {
228 printf("%s Common header CRC error\n", __func__);
229 return -EINVAL;
230 }
231
232 hdr = (struct fru_common_hdr *)addr;
Ashok Reddy Soma5cb4d032022-02-23 15:00:56 +0100233 memset((void *)&fru_data, 0, sizeof(fru_data));
Michal Simekb682f002020-11-06 13:53:01 +0100234 memcpy((void *)&fru_data, (void *)hdr,
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530235 sizeof(struct fru_common_hdr));
236
237 fru_data.captured = true;
238
239 if (hdr->off_board) {
240 addr += fru_cal_area_len(hdr->off_board);
241 fru_parse_board(addr);
242 }
243
244 env_set_hex("fru_addr", addr);
245
246 return 0;
247}
248
249static int fru_display_board(struct fru_board_data *brd, int verbose)
250{
251 u32 time = 0;
252 u8 type;
253 int len;
254 u8 *data;
255 static const char * const typecode[] = {
256 "Binary/Unspecified",
257 "BCD plus",
258 "6-bit ASCII",
259 "8-bit ASCII",
260 "2-byte UNICODE"
261 };
262 static const char * const boardinfo[] = {
263 "Manufacturer Name",
264 "Product Name",
265 "Serial No",
266 "Part Number",
Michal Simekd5c33212019-04-15 13:54:09 +0200267 "File ID",
268 /* Xilinx spec */
269 "Revision Number",
Siva Durga Prasad Paladugu7177d022019-04-10 12:38:10 +0530270 };
271
272 if (verbose) {
273 printf("*****BOARD INFO*****\n");
274 printf("Version:%d\n", fru_version(brd->ver));
275 printf("Board Area Length:%d\n", fru_cal_area_len(brd->len));
276 }
277
278 if (fru_check_language(brd->lang_code))
279 return -EINVAL;
280
281 time = brd->time[2] << 16 | brd->time[1] << 8 |
282 brd->time[0];
283
284 if (verbose)
285 printf("Time in Minutes from 0:00hrs 1/1/96: %d\n", time);
286
287 data = (u8 *)&brd->manufacturer_type_len;
288
289 for (u8 i = 0; i < (sizeof(boardinfo) / sizeof(*boardinfo)); i++) {
290 len = fru_check_type_len(*data++, brd->lang_code,
291 &type);
292 if (len == -EINVAL) {
293 printf("**** EOF for Board Area ****\n");
294 break;
295 }
296
297 if (type <= FRU_TYPELEN_TYPE_ASCII8 &&
298 (brd->lang_code == FRU_LANG_CODE_ENGLISH ||
299 brd->lang_code == FRU_LANG_CODE_ENGLISH_1))
300 debug("Type code: %s\n", typecode[type]);
301 else
302 debug("Type code: %s\n", typecode[type + 1]);
303
304 if (!len) {
305 debug("%s not found\n", boardinfo[i]);
306 continue;
307 }
308
309 switch (type) {
310 case FRU_TYPELEN_TYPE_BINARY:
311 debug("Length: %d\n", len);
312 printf(" %s: 0x%x\n", boardinfo[i], *data);
313 break;
314 case FRU_TYPELEN_TYPE_ASCII8:
315 debug("Length: %d\n", len);
316 printf(" %s: %s\n", boardinfo[i], data);
317 break;
318 default:
319 debug("Unsupported type %x\n", type);
320 }
321
322 data += FRU_BOARD_MAX_LEN;
323 }
324
325 return 0;
326}
327
328static void fru_display_common_hdr(struct fru_common_hdr *hdr, int verbose)
329{
330 if (!verbose)
331 return;
332
333 printf("*****COMMON HEADER*****\n");
334 printf("Version:%d\n", fru_version(hdr->version));
335 if (hdr->off_internal)
336 printf("Internal Use Area Offset:%d\n",
337 fru_cal_area_len(hdr->off_internal));
338 else
339 printf("*** No Internal Area ***\n");
340
341 if (hdr->off_chassis)
342 printf("Chassis Info Area Offset:%d\n",
343 fru_cal_area_len(hdr->off_chassis));
344 else
345 printf("*** No Chassis Info Area ***\n");
346
347 if (hdr->off_board)
348 printf("Board Area Offset:%d\n",
349 fru_cal_area_len(hdr->off_board));
350 else
351 printf("*** No Board Area ***\n");
352
353 if (hdr->off_product)
354 printf("Product Info Area Offset:%d\n",
355 fru_cal_area_len(hdr->off_product));
356 else
357 printf("*** No Product Info Area ***\n");
358
359 if (hdr->off_multirec)
360 printf("MultiRecord Area Offset:%d\n",
361 fru_cal_area_len(hdr->off_multirec));
362 else
363 printf("*** No MultiRecord Area ***\n");
364}
365
366int fru_display(int verbose)
367{
368 if (!fru_data.captured) {
369 printf("FRU data not available please run fru parse\n");
370 return -EINVAL;
371 }
372
373 fru_display_common_hdr(&fru_data.hdr, verbose);
374
375 return fru_display_board(&fru_data.brd, verbose);
376}