Siva Durga Prasad Paladugu | 7177d02 | 2019-04-10 12:38:10 +0530 | [diff] [blame] | 1 | // 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 | |
| 17 | struct fru_table fru_data __section(.data); |
| 18 | |
| 19 | static u16 fru_cal_area_len(u8 len) |
| 20 | { |
| 21 | return len * FRU_COMMON_HDR_LEN_MULTIPLIER; |
| 22 | } |
| 23 | |
| 24 | static u8 fru_version(u8 ver) |
| 25 | { |
| 26 | return ver & FRU_COMMON_HDR_VER_MASK; |
| 27 | } |
| 28 | |
| 29 | static 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 | |
| 39 | u8 fru_checksum(u8 *addr, u8 len) |
| 40 | { |
| 41 | u8 checksum = 0; |
| 42 | |
| 43 | while (len--) { |
| 44 | checksum += *addr; |
| 45 | addr++; |
| 46 | } |
| 47 | |
| 48 | return checksum; |
| 49 | } |
| 50 | |
| 51 | static int fru_check_type_len(u8 type_len, u8 language, u8 *type) |
| 52 | { |
| 53 | int len; |
| 54 | |
| 55 | if (type_len == FRU_TYPELEN_EOF) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | *type = (type_len & FRU_TYPELEN_CODE_MASK) >> FRU_TYPELEN_TYPE_SHIFT; |
| 59 | |
| 60 | len = type_len & FRU_TYPELEN_LEN_MASK; |
| 61 | |
| 62 | return len; |
| 63 | } |
| 64 | |
Michal Simek | d5c3321 | 2019-04-15 13:54:09 +0200 | [diff] [blame] | 65 | /* Return len */ |
| 66 | static u8 fru_gen_type_len(u8 *addr, char *name) |
| 67 | { |
| 68 | int len = strlen(name); |
| 69 | struct fru_board_info_member *member; |
| 70 | |
| 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; |
| 74 | |
| 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); |
| 78 | |
| 79 | /* Add +1 for type_len parameter */ |
| 80 | return 1 + len; |
| 81 | } |
| 82 | |
| 83 | int fru_generate(unsigned long addr, char *manufacturer, char *board_name, |
| 84 | char *serial_no, char *part_no, char *revision) |
| 85 | { |
| 86 | struct fru_common_hdr *header = (struct fru_common_hdr *)addr; |
| 87 | struct fru_board_info_header *board_info; |
| 88 | u8 *member; |
| 89 | u8 len, pad, modulo; |
| 90 | |
| 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 */ |
| 97 | header->pad = 0; |
| 98 | /* |
| 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. |
| 102 | */ |
| 103 | header->crc = 0; /* Clear before calculation */ |
| 104 | header->crc = 0 - fru_checksum((u8 *)header, sizeof(*header)); |
| 105 | |
| 106 | /* board info is just right after header */ |
| 107 | board_info = (void *)((u8 *)header + sizeof(*header)); |
| 108 | |
| 109 | debug("header %lx, board_info %lx\n", (ulong)header, (ulong)board_info); |
| 110 | |
| 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 */ |
| 116 | |
| 117 | /* Member fields are just after board_info header */ |
| 118 | member = (u8 *)board_info + sizeof(*board_info); |
| 119 | |
| 120 | len = fru_gen_type_len(member, manufacturer); /* Board Manufacturer */ |
| 121 | member += len; |
| 122 | len = fru_gen_type_len(member, board_name); /* Board Product name */ |
| 123 | member += len; |
| 124 | len = fru_gen_type_len(member, serial_no); /* Board Serial number */ |
| 125 | member += len; |
| 126 | len = fru_gen_type_len(member, part_no); /* Board part number */ |
| 127 | member += len; |
| 128 | len = fru_gen_type_len(member, "U-Boot generator"); /* File ID */ |
| 129 | member += len; |
| 130 | len = fru_gen_type_len(member, revision); /* Revision */ |
| 131 | member += len; |
| 132 | |
| 133 | *member++ = 0xc1; /* Indication of no more fields */ |
| 134 | |
| 135 | len = member - (u8 *)board_info; /* Find current length */ |
| 136 | len += 1; /* Add checksum there too for calculation */ |
| 137 | |
| 138 | modulo = len % 8; |
| 139 | |
| 140 | if (modulo) { |
| 141 | /* Do not fill last item which is checksum */ |
| 142 | for (pad = 0; pad < 8 - modulo; pad++) |
| 143 | *member++ = 0; |
| 144 | |
| 145 | /* Increase structure size */ |
| 146 | len += 8 - modulo; |
| 147 | } |
| 148 | |
| 149 | board_info->len = len / 8; /* Size in multiples of 8 bytes */ |
| 150 | |
| 151 | *member = 0; /* Clear before calculation */ |
| 152 | *member = 0 - fru_checksum((u8 *)board_info, len); |
| 153 | |
| 154 | debug("checksum %x(addr %x)\n", *member, len); |
| 155 | |
| 156 | env_set_hex("fru_addr", addr); |
| 157 | env_set_hex("filesize", (unsigned long)member - addr + 1); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Siva Durga Prasad Paladugu | 7177d02 | 2019-04-10 12:38:10 +0530 | [diff] [blame] | 162 | static int fru_parse_board(unsigned long addr) |
| 163 | { |
| 164 | u8 i, type; |
| 165 | int len; |
| 166 | u8 *data, *term; |
| 167 | |
| 168 | memcpy(&fru_data.brd.ver, (void *)addr, 6); |
| 169 | addr += 6; |
| 170 | data = (u8 *)&fru_data.brd.manufacturer_type_len; |
| 171 | |
| 172 | for (i = 0; ; i++, data += FRU_BOARD_MAX_LEN) { |
| 173 | len = fru_check_type_len(*(u8 *)addr, fru_data.brd.lang_code, |
| 174 | &type); |
| 175 | /* |
| 176 | * Stop cature if it end of fields |
| 177 | */ |
| 178 | if (len == -EINVAL) |
| 179 | break; |
| 180 | |
| 181 | /* This record type/len field */ |
| 182 | *data++ = *(u8 *)addr; |
| 183 | |
| 184 | /* Add offset to match data */ |
| 185 | addr += 1; |
| 186 | |
| 187 | /* If len is 0 it means empty field that's why skip writing */ |
| 188 | if (!len) |
| 189 | continue; |
| 190 | |
| 191 | /* Record data field */ |
| 192 | memcpy(data, (u8 *)addr, len); |
| 193 | term = data + (u8)len; |
| 194 | *term = 0; |
| 195 | addr += len; |
| 196 | } |
| 197 | |
| 198 | if (i < FRU_BOARD_AREA_TOTAL_FIELDS) { |
| 199 | printf("Board area require minimum %d fields\n", |
| 200 | FRU_BOARD_AREA_TOTAL_FIELDS); |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int fru_capture(unsigned long addr) |
| 208 | { |
| 209 | struct fru_common_hdr *hdr; |
| 210 | u8 checksum = 0; |
| 211 | |
| 212 | checksum = fru_checksum((u8 *)addr, sizeof(struct fru_common_hdr)); |
| 213 | if (checksum) { |
| 214 | printf("%s Common header CRC error\n", __func__); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | hdr = (struct fru_common_hdr *)addr; |
| 219 | |
Michal Simek | b682f00 | 2020-11-06 13:53:01 +0100 | [diff] [blame^] | 220 | memcpy((void *)&fru_data, (void *)hdr, |
Siva Durga Prasad Paladugu | 7177d02 | 2019-04-10 12:38:10 +0530 | [diff] [blame] | 221 | sizeof(struct fru_common_hdr)); |
| 222 | |
| 223 | fru_data.captured = true; |
| 224 | |
| 225 | if (hdr->off_board) { |
| 226 | addr += fru_cal_area_len(hdr->off_board); |
| 227 | fru_parse_board(addr); |
| 228 | } |
| 229 | |
| 230 | env_set_hex("fru_addr", addr); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int fru_display_board(struct fru_board_data *brd, int verbose) |
| 236 | { |
| 237 | u32 time = 0; |
| 238 | u8 type; |
| 239 | int len; |
| 240 | u8 *data; |
| 241 | static const char * const typecode[] = { |
| 242 | "Binary/Unspecified", |
| 243 | "BCD plus", |
| 244 | "6-bit ASCII", |
| 245 | "8-bit ASCII", |
| 246 | "2-byte UNICODE" |
| 247 | }; |
| 248 | static const char * const boardinfo[] = { |
| 249 | "Manufacturer Name", |
| 250 | "Product Name", |
| 251 | "Serial No", |
| 252 | "Part Number", |
Michal Simek | d5c3321 | 2019-04-15 13:54:09 +0200 | [diff] [blame] | 253 | "File ID", |
| 254 | /* Xilinx spec */ |
| 255 | "Revision Number", |
Siva Durga Prasad Paladugu | 7177d02 | 2019-04-10 12:38:10 +0530 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | if (verbose) { |
| 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)); |
| 262 | } |
| 263 | |
| 264 | if (fru_check_language(brd->lang_code)) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | time = brd->time[2] << 16 | brd->time[1] << 8 | |
| 268 | brd->time[0]; |
| 269 | |
| 270 | if (verbose) |
| 271 | printf("Time in Minutes from 0:00hrs 1/1/96: %d\n", time); |
| 272 | |
| 273 | data = (u8 *)&brd->manufacturer_type_len; |
| 274 | |
| 275 | for (u8 i = 0; i < (sizeof(boardinfo) / sizeof(*boardinfo)); i++) { |
| 276 | len = fru_check_type_len(*data++, brd->lang_code, |
| 277 | &type); |
| 278 | if (len == -EINVAL) { |
| 279 | printf("**** EOF for Board Area ****\n"); |
| 280 | break; |
| 281 | } |
| 282 | |
| 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]); |
| 287 | else |
| 288 | debug("Type code: %s\n", typecode[type + 1]); |
| 289 | |
| 290 | if (!len) { |
| 291 | debug("%s not found\n", boardinfo[i]); |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | switch (type) { |
| 296 | case FRU_TYPELEN_TYPE_BINARY: |
| 297 | debug("Length: %d\n", len); |
| 298 | printf(" %s: 0x%x\n", boardinfo[i], *data); |
| 299 | break; |
| 300 | case FRU_TYPELEN_TYPE_ASCII8: |
| 301 | debug("Length: %d\n", len); |
| 302 | printf(" %s: %s\n", boardinfo[i], data); |
| 303 | break; |
| 304 | default: |
| 305 | debug("Unsupported type %x\n", type); |
| 306 | } |
| 307 | |
| 308 | data += FRU_BOARD_MAX_LEN; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static void fru_display_common_hdr(struct fru_common_hdr *hdr, int verbose) |
| 315 | { |
| 316 | if (!verbose) |
| 317 | return; |
| 318 | |
| 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)); |
| 324 | else |
| 325 | printf("*** No Internal Area ***\n"); |
| 326 | |
| 327 | if (hdr->off_chassis) |
| 328 | printf("Chassis Info Area Offset:%d\n", |
| 329 | fru_cal_area_len(hdr->off_chassis)); |
| 330 | else |
| 331 | printf("*** No Chassis Info Area ***\n"); |
| 332 | |
| 333 | if (hdr->off_board) |
| 334 | printf("Board Area Offset:%d\n", |
| 335 | fru_cal_area_len(hdr->off_board)); |
| 336 | else |
| 337 | printf("*** No Board Area ***\n"); |
| 338 | |
| 339 | if (hdr->off_product) |
| 340 | printf("Product Info Area Offset:%d\n", |
| 341 | fru_cal_area_len(hdr->off_product)); |
| 342 | else |
| 343 | printf("*** No Product Info Area ***\n"); |
| 344 | |
| 345 | if (hdr->off_multirec) |
| 346 | printf("MultiRecord Area Offset:%d\n", |
| 347 | fru_cal_area_len(hdr->off_multirec)); |
| 348 | else |
| 349 | printf("*** No MultiRecord Area ***\n"); |
| 350 | } |
| 351 | |
| 352 | int fru_display(int verbose) |
| 353 | { |
| 354 | if (!fru_data.captured) { |
| 355 | printf("FRU data not available please run fru parse\n"); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | |
| 359 | fru_display_common_hdr(&fru_data.hdr, verbose); |
| 360 | |
| 361 | return fru_display_board(&fru_data.brd, verbose); |
| 362 | } |