1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2019 - 2020 Xilinx, Inc.
13 #include <asm/arch/hardware.h>
17 struct fru_table fru_data __section(.data);
19 static u16 fru_cal_area_len(u8 len)
21 return len * FRU_COMMON_HDR_LEN_MULTIPLIER;
24 static u8 fru_version(u8 ver)
26 return ver & FRU_COMMON_HDR_VER_MASK;
29 static int fru_check_language(u8 code)
31 if (code != FRU_LANG_CODE_ENGLISH && code != FRU_LANG_CODE_ENGLISH_1) {
32 printf("FRU_ERROR: Only English Language is supported\n");
39 u8 fru_checksum(u8 *addr, u8 len)
51 static int fru_check_type_len(u8 type_len, u8 language, u8 *type)
55 if (type_len == FRU_TYPELEN_EOF)
58 *type = (type_len & FRU_TYPELEN_CODE_MASK) >> FRU_TYPELEN_TYPE_SHIFT;
60 len = type_len & FRU_TYPELEN_LEN_MASK;
66 static u8 fru_gen_type_len(u8 *addr, char *name)
68 int len = strlen(name);
69 struct fru_board_info_member *member;
71 member = (struct fru_board_info_member *)addr;
72 member->type_len = FRU_TYPELEN_TYPE_ASCII8 << FRU_TYPELEN_TYPE_SHIFT;
73 member->type_len |= len;
75 debug("%lx/%lx: Add %s to 0x%lx (len 0x%x)\n", (ulong)addr,
76 (ulong)&member->type_len, name, (ulong)&member->name, len);
77 memcpy(&member->name, name, len);
79 /* Add +1 for type_len parameter */
83 int fru_generate(unsigned long addr, char *manufacturer, char *board_name,
84 char *serial_no, char *part_no, char *revision)
86 struct fru_common_hdr *header = (struct fru_common_hdr *)addr;
87 struct fru_board_info_header *board_info;
91 header->version = 1; /* Only version 1.0 is supported now */
92 header->off_internal = 0; /* not present */
93 header->off_chassis = 0; /* not present */
94 header->off_board = (sizeof(*header)) / 8; /* Starting offset 8 */
95 header->off_product = 0; /* not present */
96 header->off_multirec = 0; /* not present */
99 * This unsigned byte can be used to calculate a zero checksum
100 * for the data area following the header. I.e. the modulo 256 sum of
101 * the record data bytes plus the checksum byte equals zero.
103 header->crc = 0; /* Clear before calculation */
104 header->crc = 0 - fru_checksum((u8 *)header, sizeof(*header));
106 /* board info is just right after header */
107 board_info = (void *)((u8 *)header + sizeof(*header));
109 debug("header %lx, board_info %lx\n", (ulong)header, (ulong)board_info);
111 board_info->ver = 1; /* 1.0 spec */
112 board_info->lang_code = 0; /* English */
113 board_info->time[0] = 0; /* unspecified */
114 board_info->time[1] = 0; /* unspecified */
115 board_info->time[2] = 0; /* unspecified */
117 /* Member fields are just after board_info header */
118 member = (u8 *)board_info + sizeof(*board_info);
120 len = fru_gen_type_len(member, manufacturer); /* Board Manufacturer */
122 len = fru_gen_type_len(member, board_name); /* Board Product name */
124 len = fru_gen_type_len(member, serial_no); /* Board Serial number */
126 len = fru_gen_type_len(member, part_no); /* Board part number */
128 len = fru_gen_type_len(member, "U-Boot generator"); /* File ID */
130 len = fru_gen_type_len(member, revision); /* Revision */
133 *member++ = 0xc1; /* Indication of no more fields */
135 len = member - (u8 *)board_info; /* Find current length */
136 len += 1; /* Add checksum there too for calculation */
141 /* Do not fill last item which is checksum */
142 for (pad = 0; pad < 8 - modulo; pad++)
145 /* Increase structure size */
149 board_info->len = len / 8; /* Size in multiples of 8 bytes */
151 *member = 0; /* Clear before calculation */
152 *member = 0 - fru_checksum((u8 *)board_info, len);
154 debug("checksum %x(addr %x)\n", *member, len);
156 env_set_hex("fru_addr", addr);
157 env_set_hex("filesize", (unsigned long)member - addr + 1);
162 static int fru_parse_board(unsigned long addr)
168 memcpy(&fru_data.brd.ver, (void *)addr, 6);
170 data = (u8 *)&fru_data.brd.manufacturer_type_len;
172 for (i = 0; ; i++, data += FRU_BOARD_MAX_LEN) {
173 len = fru_check_type_len(*(u8 *)addr, fru_data.brd.lang_code,
176 * Stop cature if it end of fields
181 /* This record type/len field */
182 *data++ = *(u8 *)addr;
184 /* Add offset to match data */
187 /* If len is 0 it means empty field that's why skip writing */
191 /* Record data field */
192 memcpy(data, (u8 *)addr, len);
193 term = data + (u8)len;
198 if (i < FRU_BOARD_AREA_TOTAL_FIELDS) {
199 printf("Board area require minimum %d fields\n",
200 FRU_BOARD_AREA_TOTAL_FIELDS);
207 int fru_capture(unsigned long addr)
209 struct fru_common_hdr *hdr;
212 checksum = fru_checksum((u8 *)addr, sizeof(struct fru_common_hdr));
214 printf("%s Common header CRC error\n", __func__);
218 hdr = (struct fru_common_hdr *)addr;
220 memcpy((void *)&fru_data.hdr, (void *)hdr,
221 sizeof(struct fru_common_hdr));
223 fru_data.captured = true;
225 if (hdr->off_board) {
226 addr += fru_cal_area_len(hdr->off_board);
227 fru_parse_board(addr);
230 env_set_hex("fru_addr", addr);
235 static int fru_display_board(struct fru_board_data *brd, int verbose)
241 static const char * const typecode[] = {
242 "Binary/Unspecified",
248 static const char * const boardinfo[] = {
259 printf("*****BOARD INFO*****\n");
260 printf("Version:%d\n", fru_version(brd->ver));
261 printf("Board Area Length:%d\n", fru_cal_area_len(brd->len));
264 if (fru_check_language(brd->lang_code))
267 time = brd->time[2] << 16 | brd->time[1] << 8 |
271 printf("Time in Minutes from 0:00hrs 1/1/96: %d\n", time);
273 data = (u8 *)&brd->manufacturer_type_len;
275 for (u8 i = 0; i < (sizeof(boardinfo) / sizeof(*boardinfo)); i++) {
276 len = fru_check_type_len(*data++, brd->lang_code,
278 if (len == -EINVAL) {
279 printf("**** EOF for Board Area ****\n");
283 if (type <= FRU_TYPELEN_TYPE_ASCII8 &&
284 (brd->lang_code == FRU_LANG_CODE_ENGLISH ||
285 brd->lang_code == FRU_LANG_CODE_ENGLISH_1))
286 debug("Type code: %s\n", typecode[type]);
288 debug("Type code: %s\n", typecode[type + 1]);
291 debug("%s not found\n", boardinfo[i]);
296 case FRU_TYPELEN_TYPE_BINARY:
297 debug("Length: %d\n", len);
298 printf(" %s: 0x%x\n", boardinfo[i], *data);
300 case FRU_TYPELEN_TYPE_ASCII8:
301 debug("Length: %d\n", len);
302 printf(" %s: %s\n", boardinfo[i], data);
305 debug("Unsupported type %x\n", type);
308 data += FRU_BOARD_MAX_LEN;
314 static void fru_display_common_hdr(struct fru_common_hdr *hdr, int verbose)
319 printf("*****COMMON HEADER*****\n");
320 printf("Version:%d\n", fru_version(hdr->version));
321 if (hdr->off_internal)
322 printf("Internal Use Area Offset:%d\n",
323 fru_cal_area_len(hdr->off_internal));
325 printf("*** No Internal Area ***\n");
327 if (hdr->off_chassis)
328 printf("Chassis Info Area Offset:%d\n",
329 fru_cal_area_len(hdr->off_chassis));
331 printf("*** No Chassis Info Area ***\n");
334 printf("Board Area Offset:%d\n",
335 fru_cal_area_len(hdr->off_board));
337 printf("*** No Board Area ***\n");
339 if (hdr->off_product)
340 printf("Product Info Area Offset:%d\n",
341 fru_cal_area_len(hdr->off_product));
343 printf("*** No Product Info Area ***\n");
345 if (hdr->off_multirec)
346 printf("MultiRecord Area Offset:%d\n",
347 fru_cal_area_len(hdr->off_multirec));
349 printf("*** No MultiRecord Area ***\n");
352 int fru_display(int verbose)
354 if (!fru_data.captured) {
355 printf("FRU data not available please run fru parse\n");
359 fru_display_common_hdr(&fru_data.hdr, verbose);
361 return fru_display_board(&fru_data.brd, verbose);