Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/types.h>
0003 #include <linux/string.h>
0004 #include <linux/init.h>
0005 #include <linux/module.h>
0006 #include <linux/ctype.h>
0007 #include <linux/dmi.h>
0008 #include <linux/efi.h>
0009 #include <linux/memblock.h>
0010 #include <linux/random.h>
0011 #include <asm/dmi.h>
0012 #include <asm/unaligned.h>
0013 
0014 #ifndef SMBIOS_ENTRY_POINT_SCAN_START
0015 #define SMBIOS_ENTRY_POINT_SCAN_START 0xF0000
0016 #endif
0017 
0018 struct kobject *dmi_kobj;
0019 EXPORT_SYMBOL_GPL(dmi_kobj);
0020 
0021 /*
0022  * DMI stands for "Desktop Management Interface".  It is part
0023  * of and an antecedent to, SMBIOS, which stands for System
0024  * Management BIOS.  See further: https://www.dmtf.org/standards
0025  */
0026 static const char dmi_empty_string[] = "";
0027 
0028 static u32 dmi_ver __initdata;
0029 static u32 dmi_len;
0030 static u16 dmi_num;
0031 static u8 smbios_entry_point[32];
0032 static int smbios_entry_point_size;
0033 
0034 /* DMI system identification string used during boot */
0035 static char dmi_ids_string[128] __initdata;
0036 
0037 static struct dmi_memdev_info {
0038     const char *device;
0039     const char *bank;
0040     u64 size;       /* bytes */
0041     u16 handle;
0042     u8 type;        /* DDR2, DDR3, DDR4 etc */
0043 } *dmi_memdev;
0044 static int dmi_memdev_nr;
0045 
0046 static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
0047 {
0048     const u8 *bp = ((u8 *) dm) + dm->length;
0049     const u8 *nsp;
0050 
0051     if (s) {
0052         while (--s > 0 && *bp)
0053             bp += strlen(bp) + 1;
0054 
0055         /* Strings containing only spaces are considered empty */
0056         nsp = bp;
0057         while (*nsp == ' ')
0058             nsp++;
0059         if (*nsp != '\0')
0060             return bp;
0061     }
0062 
0063     return dmi_empty_string;
0064 }
0065 
0066 static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
0067 {
0068     const char *bp = dmi_string_nosave(dm, s);
0069     char *str;
0070     size_t len;
0071 
0072     if (bp == dmi_empty_string)
0073         return dmi_empty_string;
0074 
0075     len = strlen(bp) + 1;
0076     str = dmi_alloc(len);
0077     if (str != NULL)
0078         strcpy(str, bp);
0079 
0080     return str;
0081 }
0082 
0083 /*
0084  *  We have to be cautious here. We have seen BIOSes with DMI pointers
0085  *  pointing to completely the wrong place for example
0086  */
0087 static void dmi_decode_table(u8 *buf,
0088                  void (*decode)(const struct dmi_header *, void *),
0089                  void *private_data)
0090 {
0091     u8 *data = buf;
0092     int i = 0;
0093 
0094     /*
0095      * Stop when we have seen all the items the table claimed to have
0096      * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS
0097      * >= 3.0 only) OR we run off the end of the table (should never
0098      * happen but sometimes does on bogus implementations.)
0099      */
0100     while ((!dmi_num || i < dmi_num) &&
0101            (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
0102         const struct dmi_header *dm = (const struct dmi_header *)data;
0103 
0104         /*
0105          *  We want to know the total length (formatted area and
0106          *  strings) before decoding to make sure we won't run off the
0107          *  table in dmi_decode or dmi_string
0108          */
0109         data += dm->length;
0110         while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
0111             data++;
0112         if (data - buf < dmi_len - 1)
0113             decode(dm, private_data);
0114 
0115         data += 2;
0116         i++;
0117 
0118         /*
0119          * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
0120          * For tables behind a 64-bit entry point, we have no item
0121          * count and no exact table length, so stop on end-of-table
0122          * marker. For tables behind a 32-bit entry point, we have
0123          * seen OEM structures behind the end-of-table marker on
0124          * some systems, so don't trust it.
0125          */
0126         if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE)
0127             break;
0128     }
0129 
0130     /* Trim DMI table length if needed */
0131     if (dmi_len > data - buf)
0132         dmi_len = data - buf;
0133 }
0134 
0135 static phys_addr_t dmi_base;
0136 
0137 static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
0138         void *))
0139 {
0140     u8 *buf;
0141     u32 orig_dmi_len = dmi_len;
0142 
0143     buf = dmi_early_remap(dmi_base, orig_dmi_len);
0144     if (buf == NULL)
0145         return -ENOMEM;
0146 
0147     dmi_decode_table(buf, decode, NULL);
0148 
0149     add_device_randomness(buf, dmi_len);
0150 
0151     dmi_early_unmap(buf, orig_dmi_len);
0152     return 0;
0153 }
0154 
0155 static int __init dmi_checksum(const u8 *buf, u8 len)
0156 {
0157     u8 sum = 0;
0158     int a;
0159 
0160     for (a = 0; a < len; a++)
0161         sum += buf[a];
0162 
0163     return sum == 0;
0164 }
0165 
0166 static const char *dmi_ident[DMI_STRING_MAX];
0167 static LIST_HEAD(dmi_devices);
0168 int dmi_available;
0169 EXPORT_SYMBOL_GPL(dmi_available);
0170 
0171 /*
0172  *  Save a DMI string
0173  */
0174 static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
0175         int string)
0176 {
0177     const char *d = (const char *) dm;
0178     const char *p;
0179 
0180     if (dmi_ident[slot] || dm->length <= string)
0181         return;
0182 
0183     p = dmi_string(dm, d[string]);
0184     if (p == NULL)
0185         return;
0186 
0187     dmi_ident[slot] = p;
0188 }
0189 
0190 static void __init dmi_save_release(const struct dmi_header *dm, int slot,
0191         int index)
0192 {
0193     const u8 *minor, *major;
0194     char *s;
0195 
0196     /* If the table doesn't have the field, let's return */
0197     if (dmi_ident[slot] || dm->length < index)
0198         return;
0199 
0200     minor = (u8 *) dm + index;
0201     major = (u8 *) dm + index - 1;
0202 
0203     /* As per the spec, if the system doesn't support this field,
0204      * the value is FF
0205      */
0206     if (*major == 0xFF && *minor == 0xFF)
0207         return;
0208 
0209     s = dmi_alloc(8);
0210     if (!s)
0211         return;
0212 
0213     sprintf(s, "%u.%u", *major, *minor);
0214 
0215     dmi_ident[slot] = s;
0216 }
0217 
0218 static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
0219         int index)
0220 {
0221     const u8 *d;
0222     char *s;
0223     int is_ff = 1, is_00 = 1, i;
0224 
0225     if (dmi_ident[slot] || dm->length < index + 16)
0226         return;
0227 
0228     d = (u8 *) dm + index;
0229     for (i = 0; i < 16 && (is_ff || is_00); i++) {
0230         if (d[i] != 0x00)
0231             is_00 = 0;
0232         if (d[i] != 0xFF)
0233             is_ff = 0;
0234     }
0235 
0236     if (is_ff || is_00)
0237         return;
0238 
0239     s = dmi_alloc(16*2+4+1);
0240     if (!s)
0241         return;
0242 
0243     /*
0244      * As of version 2.6 of the SMBIOS specification, the first 3 fields of
0245      * the UUID are supposed to be little-endian encoded.  The specification
0246      * says that this is the defacto standard.
0247      */
0248     if (dmi_ver >= 0x020600)
0249         sprintf(s, "%pUl", d);
0250     else
0251         sprintf(s, "%pUb", d);
0252 
0253     dmi_ident[slot] = s;
0254 }
0255 
0256 static void __init dmi_save_type(const struct dmi_header *dm, int slot,
0257         int index)
0258 {
0259     const u8 *d;
0260     char *s;
0261 
0262     if (dmi_ident[slot] || dm->length <= index)
0263         return;
0264 
0265     s = dmi_alloc(4);
0266     if (!s)
0267         return;
0268 
0269     d = (u8 *) dm + index;
0270     sprintf(s, "%u", *d & 0x7F);
0271     dmi_ident[slot] = s;
0272 }
0273 
0274 static void __init dmi_save_one_device(int type, const char *name)
0275 {
0276     struct dmi_device *dev;
0277 
0278     /* No duplicate device */
0279     if (dmi_find_device(type, name, NULL))
0280         return;
0281 
0282     dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
0283     if (!dev)
0284         return;
0285 
0286     dev->type = type;
0287     strcpy((char *)(dev + 1), name);
0288     dev->name = (char *)(dev + 1);
0289     dev->device_data = NULL;
0290     list_add(&dev->list, &dmi_devices);
0291 }
0292 
0293 static void __init dmi_save_devices(const struct dmi_header *dm)
0294 {
0295     int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
0296 
0297     for (i = 0; i < count; i++) {
0298         const char *d = (char *)(dm + 1) + (i * 2);
0299 
0300         /* Skip disabled device */
0301         if ((*d & 0x80) == 0)
0302             continue;
0303 
0304         dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
0305     }
0306 }
0307 
0308 static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
0309 {
0310     int i, count;
0311     struct dmi_device *dev;
0312 
0313     if (dm->length < 0x05)
0314         return;
0315 
0316     count = *(u8 *)(dm + 1);
0317     for (i = 1; i <= count; i++) {
0318         const char *devname = dmi_string(dm, i);
0319 
0320         if (devname == dmi_empty_string)
0321             continue;
0322 
0323         dev = dmi_alloc(sizeof(*dev));
0324         if (!dev)
0325             break;
0326 
0327         dev->type = DMI_DEV_TYPE_OEM_STRING;
0328         dev->name = devname;
0329         dev->device_data = NULL;
0330 
0331         list_add(&dev->list, &dmi_devices);
0332     }
0333 }
0334 
0335 static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
0336 {
0337     struct dmi_device *dev;
0338     void *data;
0339 
0340     data = dmi_alloc(dm->length);
0341     if (data == NULL)
0342         return;
0343 
0344     memcpy(data, dm, dm->length);
0345 
0346     dev = dmi_alloc(sizeof(*dev));
0347     if (!dev)
0348         return;
0349 
0350     dev->type = DMI_DEV_TYPE_IPMI;
0351     dev->name = "IPMI controller";
0352     dev->device_data = data;
0353 
0354     list_add_tail(&dev->list, &dmi_devices);
0355 }
0356 
0357 static void __init dmi_save_dev_pciaddr(int instance, int segment, int bus,
0358                     int devfn, const char *name, int type)
0359 {
0360     struct dmi_dev_onboard *dev;
0361 
0362     /* Ignore invalid values */
0363     if (type == DMI_DEV_TYPE_DEV_SLOT &&
0364         segment == 0xFFFF && bus == 0xFF && devfn == 0xFF)
0365         return;
0366 
0367     dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
0368     if (!dev)
0369         return;
0370 
0371     dev->instance = instance;
0372     dev->segment = segment;
0373     dev->bus = bus;
0374     dev->devfn = devfn;
0375 
0376     strcpy((char *)&dev[1], name);
0377     dev->dev.type = type;
0378     dev->dev.name = (char *)&dev[1];
0379     dev->dev.device_data = dev;
0380 
0381     list_add(&dev->dev.list, &dmi_devices);
0382 }
0383 
0384 static void __init dmi_save_extended_devices(const struct dmi_header *dm)
0385 {
0386     const char *name;
0387     const u8 *d = (u8 *)dm;
0388 
0389     if (dm->length < 0x0B)
0390         return;
0391 
0392     /* Skip disabled device */
0393     if ((d[0x5] & 0x80) == 0)
0394         return;
0395 
0396     name = dmi_string_nosave(dm, d[0x4]);
0397     dmi_save_dev_pciaddr(d[0x6], *(u16 *)(d + 0x7), d[0x9], d[0xA], name,
0398                  DMI_DEV_TYPE_DEV_ONBOARD);
0399     dmi_save_one_device(d[0x5] & 0x7f, name);
0400 }
0401 
0402 static void __init dmi_save_system_slot(const struct dmi_header *dm)
0403 {
0404     const u8 *d = (u8 *)dm;
0405 
0406     /* Need SMBIOS 2.6+ structure */
0407     if (dm->length < 0x11)
0408         return;
0409     dmi_save_dev_pciaddr(*(u16 *)(d + 0x9), *(u16 *)(d + 0xD), d[0xF],
0410                  d[0x10], dmi_string_nosave(dm, d[0x4]),
0411                  DMI_DEV_TYPE_DEV_SLOT);
0412 }
0413 
0414 static void __init count_mem_devices(const struct dmi_header *dm, void *v)
0415 {
0416     if (dm->type != DMI_ENTRY_MEM_DEVICE)
0417         return;
0418     dmi_memdev_nr++;
0419 }
0420 
0421 static void __init save_mem_devices(const struct dmi_header *dm, void *v)
0422 {
0423     const char *d = (const char *)dm;
0424     static int nr;
0425     u64 bytes;
0426     u16 size;
0427 
0428     if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x13)
0429         return;
0430     if (nr >= dmi_memdev_nr) {
0431         pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
0432         return;
0433     }
0434     dmi_memdev[nr].handle = get_unaligned(&dm->handle);
0435     dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
0436     dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
0437     dmi_memdev[nr].type = d[0x12];
0438 
0439     size = get_unaligned((u16 *)&d[0xC]);
0440     if (size == 0)
0441         bytes = 0;
0442     else if (size == 0xffff)
0443         bytes = ~0ull;
0444     else if (size & 0x8000)
0445         bytes = (u64)(size & 0x7fff) << 10;
0446     else if (size != 0x7fff || dm->length < 0x20)
0447         bytes = (u64)size << 20;
0448     else
0449         bytes = (u64)get_unaligned((u32 *)&d[0x1C]) << 20;
0450 
0451     dmi_memdev[nr].size = bytes;
0452     nr++;
0453 }
0454 
0455 static void __init dmi_memdev_walk(void)
0456 {
0457     if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
0458         dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
0459         if (dmi_memdev)
0460             dmi_walk_early(save_mem_devices);
0461     }
0462 }
0463 
0464 /*
0465  *  Process a DMI table entry. Right now all we care about are the BIOS
0466  *  and machine entries. For 2.5 we should pull the smbus controller info
0467  *  out of here.
0468  */
0469 static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
0470 {
0471     switch (dm->type) {
0472     case 0:     /* BIOS Information */
0473         dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
0474         dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
0475         dmi_save_ident(dm, DMI_BIOS_DATE, 8);
0476         dmi_save_release(dm, DMI_BIOS_RELEASE, 21);
0477         dmi_save_release(dm, DMI_EC_FIRMWARE_RELEASE, 23);
0478         break;
0479     case 1:     /* System Information */
0480         dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
0481         dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
0482         dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
0483         dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
0484         dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
0485         dmi_save_ident(dm, DMI_PRODUCT_SKU, 25);
0486         dmi_save_ident(dm, DMI_PRODUCT_FAMILY, 26);
0487         break;
0488     case 2:     /* Base Board Information */
0489         dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
0490         dmi_save_ident(dm, DMI_BOARD_NAME, 5);
0491         dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
0492         dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
0493         dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
0494         break;
0495     case 3:     /* Chassis Information */
0496         dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
0497         dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
0498         dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
0499         dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
0500         dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
0501         break;
0502     case 9:     /* System Slots */
0503         dmi_save_system_slot(dm);
0504         break;
0505     case 10:    /* Onboard Devices Information */
0506         dmi_save_devices(dm);
0507         break;
0508     case 11:    /* OEM Strings */
0509         dmi_save_oem_strings_devices(dm);
0510         break;
0511     case 38:    /* IPMI Device Information */
0512         dmi_save_ipmi_device(dm);
0513         break;
0514     case 41:    /* Onboard Devices Extended Information */
0515         dmi_save_extended_devices(dm);
0516     }
0517 }
0518 
0519 static int __init print_filtered(char *buf, size_t len, const char *info)
0520 {
0521     int c = 0;
0522     const char *p;
0523 
0524     if (!info)
0525         return c;
0526 
0527     for (p = info; *p; p++)
0528         if (isprint(*p))
0529             c += scnprintf(buf + c, len - c, "%c", *p);
0530         else
0531             c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
0532     return c;
0533 }
0534 
0535 static void __init dmi_format_ids(char *buf, size_t len)
0536 {
0537     int c = 0;
0538     const char *board;  /* Board Name is optional */
0539 
0540     c += print_filtered(buf + c, len - c,
0541                 dmi_get_system_info(DMI_SYS_VENDOR));
0542     c += scnprintf(buf + c, len - c, " ");
0543     c += print_filtered(buf + c, len - c,
0544                 dmi_get_system_info(DMI_PRODUCT_NAME));
0545 
0546     board = dmi_get_system_info(DMI_BOARD_NAME);
0547     if (board) {
0548         c += scnprintf(buf + c, len - c, "/");
0549         c += print_filtered(buf + c, len - c, board);
0550     }
0551     c += scnprintf(buf + c, len - c, ", BIOS ");
0552     c += print_filtered(buf + c, len - c,
0553                 dmi_get_system_info(DMI_BIOS_VERSION));
0554     c += scnprintf(buf + c, len - c, " ");
0555     c += print_filtered(buf + c, len - c,
0556                 dmi_get_system_info(DMI_BIOS_DATE));
0557 }
0558 
0559 /*
0560  * Check for DMI/SMBIOS headers in the system firmware image.  Any
0561  * SMBIOS header must start 16 bytes before the DMI header, so take a
0562  * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
0563  * 0.  If the DMI header is present, set dmi_ver accordingly (SMBIOS
0564  * takes precedence) and return 0.  Otherwise return 1.
0565  */
0566 static int __init dmi_present(const u8 *buf)
0567 {
0568     u32 smbios_ver;
0569 
0570     if (memcmp(buf, "_SM_", 4) == 0 &&
0571         buf[5] < 32 && dmi_checksum(buf, buf[5])) {
0572         smbios_ver = get_unaligned_be16(buf + 6);
0573         smbios_entry_point_size = buf[5];
0574         memcpy(smbios_entry_point, buf, smbios_entry_point_size);
0575 
0576         /* Some BIOS report weird SMBIOS version, fix that up */
0577         switch (smbios_ver) {
0578         case 0x021F:
0579         case 0x0221:
0580             pr_debug("SMBIOS version fixup (2.%d->2.%d)\n",
0581                  smbios_ver & 0xFF, 3);
0582             smbios_ver = 0x0203;
0583             break;
0584         case 0x0233:
0585             pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 51, 6);
0586             smbios_ver = 0x0206;
0587             break;
0588         }
0589     } else {
0590         smbios_ver = 0;
0591     }
0592 
0593     buf += 16;
0594 
0595     if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
0596         if (smbios_ver)
0597             dmi_ver = smbios_ver;
0598         else
0599             dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
0600         dmi_ver <<= 8;
0601         dmi_num = get_unaligned_le16(buf + 12);
0602         dmi_len = get_unaligned_le16(buf + 6);
0603         dmi_base = get_unaligned_le32(buf + 8);
0604 
0605         if (dmi_walk_early(dmi_decode) == 0) {
0606             if (smbios_ver) {
0607                 pr_info("SMBIOS %d.%d present.\n",
0608                     dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
0609             } else {
0610                 smbios_entry_point_size = 15;
0611                 memcpy(smbios_entry_point, buf,
0612                        smbios_entry_point_size);
0613                 pr_info("Legacy DMI %d.%d present.\n",
0614                     dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
0615             }
0616             dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
0617             pr_info("DMI: %s\n", dmi_ids_string);
0618             return 0;
0619         }
0620     }
0621 
0622     return 1;
0623 }
0624 
0625 /*
0626  * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
0627  * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
0628  */
0629 static int __init dmi_smbios3_present(const u8 *buf)
0630 {
0631     if (memcmp(buf, "_SM3_", 5) == 0 &&
0632         buf[6] < 32 && dmi_checksum(buf, buf[6])) {
0633         dmi_ver = get_unaligned_be24(buf + 7);
0634         dmi_num = 0;            /* No longer specified */
0635         dmi_len = get_unaligned_le32(buf + 12);
0636         dmi_base = get_unaligned_le64(buf + 16);
0637         smbios_entry_point_size = buf[6];
0638         memcpy(smbios_entry_point, buf, smbios_entry_point_size);
0639 
0640         if (dmi_walk_early(dmi_decode) == 0) {
0641             pr_info("SMBIOS %d.%d.%d present.\n",
0642                 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
0643                 dmi_ver & 0xFF);
0644             dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
0645             pr_info("DMI: %s\n", dmi_ids_string);
0646             return 0;
0647         }
0648     }
0649     return 1;
0650 }
0651 
0652 static void __init dmi_scan_machine(void)
0653 {
0654     char __iomem *p, *q;
0655     char buf[32];
0656 
0657     if (efi_enabled(EFI_CONFIG_TABLES)) {
0658         /*
0659          * According to the DMTF SMBIOS reference spec v3.0.0, it is
0660          * allowed to define both the 64-bit entry point (smbios3) and
0661          * the 32-bit entry point (smbios), in which case they should
0662          * either both point to the same SMBIOS structure table, or the
0663          * table pointed to by the 64-bit entry point should contain a
0664          * superset of the table contents pointed to by the 32-bit entry
0665          * point (section 5.2)
0666          * This implies that the 64-bit entry point should have
0667          * precedence if it is defined and supported by the OS. If we
0668          * have the 64-bit entry point, but fail to decode it, fall
0669          * back to the legacy one (if available)
0670          */
0671         if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
0672             p = dmi_early_remap(efi.smbios3, 32);
0673             if (p == NULL)
0674                 goto error;
0675             memcpy_fromio(buf, p, 32);
0676             dmi_early_unmap(p, 32);
0677 
0678             if (!dmi_smbios3_present(buf)) {
0679                 dmi_available = 1;
0680                 return;
0681             }
0682         }
0683         if (efi.smbios == EFI_INVALID_TABLE_ADDR)
0684             goto error;
0685 
0686         /* This is called as a core_initcall() because it isn't
0687          * needed during early boot.  This also means we can
0688          * iounmap the space when we're done with it.
0689          */
0690         p = dmi_early_remap(efi.smbios, 32);
0691         if (p == NULL)
0692             goto error;
0693         memcpy_fromio(buf, p, 32);
0694         dmi_early_unmap(p, 32);
0695 
0696         if (!dmi_present(buf)) {
0697             dmi_available = 1;
0698             return;
0699         }
0700     } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
0701         p = dmi_early_remap(SMBIOS_ENTRY_POINT_SCAN_START, 0x10000);
0702         if (p == NULL)
0703             goto error;
0704 
0705         /*
0706          * Same logic as above, look for a 64-bit entry point
0707          * first, and if not found, fall back to 32-bit entry point.
0708          */
0709         memcpy_fromio(buf, p, 16);
0710         for (q = p + 16; q < p + 0x10000; q += 16) {
0711             memcpy_fromio(buf + 16, q, 16);
0712             if (!dmi_smbios3_present(buf)) {
0713                 dmi_available = 1;
0714                 dmi_early_unmap(p, 0x10000);
0715                 return;
0716             }
0717             memcpy(buf, buf + 16, 16);
0718         }
0719 
0720         /*
0721          * Iterate over all possible DMI header addresses q.
0722          * Maintain the 32 bytes around q in buf.  On the
0723          * first iteration, substitute zero for the
0724          * out-of-range bytes so there is no chance of falsely
0725          * detecting an SMBIOS header.
0726          */
0727         memset(buf, 0, 16);
0728         for (q = p; q < p + 0x10000; q += 16) {
0729             memcpy_fromio(buf + 16, q, 16);
0730             if (!dmi_present(buf)) {
0731                 dmi_available = 1;
0732                 dmi_early_unmap(p, 0x10000);
0733                 return;
0734             }
0735             memcpy(buf, buf + 16, 16);
0736         }
0737         dmi_early_unmap(p, 0x10000);
0738     }
0739  error:
0740     pr_info("DMI not present or invalid.\n");
0741 }
0742 
0743 static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
0744                   struct bin_attribute *attr, char *buf,
0745                   loff_t pos, size_t count)
0746 {
0747     memcpy(buf, attr->private + pos, count);
0748     return count;
0749 }
0750 
0751 static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
0752 static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
0753 
0754 static int __init dmi_init(void)
0755 {
0756     struct kobject *tables_kobj;
0757     u8 *dmi_table;
0758     int ret = -ENOMEM;
0759 
0760     if (!dmi_available)
0761         return 0;
0762 
0763     /*
0764      * Set up dmi directory at /sys/firmware/dmi. This entry should stay
0765      * even after farther error, as it can be used by other modules like
0766      * dmi-sysfs.
0767      */
0768     dmi_kobj = kobject_create_and_add("dmi", firmware_kobj);
0769     if (!dmi_kobj)
0770         goto err;
0771 
0772     tables_kobj = kobject_create_and_add("tables", dmi_kobj);
0773     if (!tables_kobj)
0774         goto err;
0775 
0776     dmi_table = dmi_remap(dmi_base, dmi_len);
0777     if (!dmi_table)
0778         goto err_tables;
0779 
0780     bin_attr_smbios_entry_point.size = smbios_entry_point_size;
0781     bin_attr_smbios_entry_point.private = smbios_entry_point;
0782     ret = sysfs_create_bin_file(tables_kobj, &bin_attr_smbios_entry_point);
0783     if (ret)
0784         goto err_unmap;
0785 
0786     bin_attr_DMI.size = dmi_len;
0787     bin_attr_DMI.private = dmi_table;
0788     ret = sysfs_create_bin_file(tables_kobj, &bin_attr_DMI);
0789     if (!ret)
0790         return 0;
0791 
0792     sysfs_remove_bin_file(tables_kobj,
0793                   &bin_attr_smbios_entry_point);
0794  err_unmap:
0795     dmi_unmap(dmi_table);
0796  err_tables:
0797     kobject_del(tables_kobj);
0798     kobject_put(tables_kobj);
0799  err:
0800     pr_err("dmi: Firmware registration failed.\n");
0801 
0802     return ret;
0803 }
0804 subsys_initcall(dmi_init);
0805 
0806 /**
0807  *  dmi_setup - scan and setup DMI system information
0808  *
0809  *  Scan the DMI system information. This setups DMI identifiers
0810  *  (dmi_system_id) for printing it out on task dumps and prepares
0811  *  DIMM entry information (dmi_memdev_info) from the SMBIOS table
0812  *  for using this when reporting memory errors.
0813  */
0814 void __init dmi_setup(void)
0815 {
0816     dmi_scan_machine();
0817     if (!dmi_available)
0818         return;
0819 
0820     dmi_memdev_walk();
0821     dump_stack_set_arch_desc("%s", dmi_ids_string);
0822 }
0823 
0824 /**
0825  *  dmi_matches - check if dmi_system_id structure matches system DMI data
0826  *  @dmi: pointer to the dmi_system_id structure to check
0827  */
0828 static bool dmi_matches(const struct dmi_system_id *dmi)
0829 {
0830     int i;
0831 
0832     for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
0833         int s = dmi->matches[i].slot;
0834         if (s == DMI_NONE)
0835             break;
0836         if (s == DMI_OEM_STRING) {
0837             /* DMI_OEM_STRING must be exact match */
0838             const struct dmi_device *valid;
0839 
0840             valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING,
0841                         dmi->matches[i].substr, NULL);
0842             if (valid)
0843                 continue;
0844         } else if (dmi_ident[s]) {
0845             if (dmi->matches[i].exact_match) {
0846                 if (!strcmp(dmi_ident[s],
0847                         dmi->matches[i].substr))
0848                     continue;
0849             } else {
0850                 if (strstr(dmi_ident[s],
0851                        dmi->matches[i].substr))
0852                     continue;
0853             }
0854         }
0855 
0856         /* No match */
0857         return false;
0858     }
0859     return true;
0860 }
0861 
0862 /**
0863  *  dmi_is_end_of_table - check for end-of-table marker
0864  *  @dmi: pointer to the dmi_system_id structure to check
0865  */
0866 static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
0867 {
0868     return dmi->matches[0].slot == DMI_NONE;
0869 }
0870 
0871 /**
0872  *  dmi_check_system - check system DMI data
0873  *  @list: array of dmi_system_id structures to match against
0874  *      All non-null elements of the list must match
0875  *      their slot's (field index's) data (i.e., each
0876  *      list string must be a substring of the specified
0877  *      DMI slot's string data) to be considered a
0878  *      successful match.
0879  *
0880  *  Walk the blacklist table running matching functions until someone
0881  *  returns non zero or we hit the end. Callback function is called for
0882  *  each successful match. Returns the number of matches.
0883  *
0884  *  dmi_setup must be called before this function is called.
0885  */
0886 int dmi_check_system(const struct dmi_system_id *list)
0887 {
0888     int count = 0;
0889     const struct dmi_system_id *d;
0890 
0891     for (d = list; !dmi_is_end_of_table(d); d++)
0892         if (dmi_matches(d)) {
0893             count++;
0894             if (d->callback && d->callback(d))
0895                 break;
0896         }
0897 
0898     return count;
0899 }
0900 EXPORT_SYMBOL(dmi_check_system);
0901 
0902 /**
0903  *  dmi_first_match - find dmi_system_id structure matching system DMI data
0904  *  @list: array of dmi_system_id structures to match against
0905  *      All non-null elements of the list must match
0906  *      their slot's (field index's) data (i.e., each
0907  *      list string must be a substring of the specified
0908  *      DMI slot's string data) to be considered a
0909  *      successful match.
0910  *
0911  *  Walk the blacklist table until the first match is found.  Return the
0912  *  pointer to the matching entry or NULL if there's no match.
0913  *
0914  *  dmi_setup must be called before this function is called.
0915  */
0916 const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
0917 {
0918     const struct dmi_system_id *d;
0919 
0920     for (d = list; !dmi_is_end_of_table(d); d++)
0921         if (dmi_matches(d))
0922             return d;
0923 
0924     return NULL;
0925 }
0926 EXPORT_SYMBOL(dmi_first_match);
0927 
0928 /**
0929  *  dmi_get_system_info - return DMI data value
0930  *  @field: data index (see enum dmi_field)
0931  *
0932  *  Returns one DMI data value, can be used to perform
0933  *  complex DMI data checks.
0934  */
0935 const char *dmi_get_system_info(int field)
0936 {
0937     return dmi_ident[field];
0938 }
0939 EXPORT_SYMBOL(dmi_get_system_info);
0940 
0941 /**
0942  * dmi_name_in_serial - Check if string is in the DMI product serial information
0943  * @str: string to check for
0944  */
0945 int dmi_name_in_serial(const char *str)
0946 {
0947     int f = DMI_PRODUCT_SERIAL;
0948     if (dmi_ident[f] && strstr(dmi_ident[f], str))
0949         return 1;
0950     return 0;
0951 }
0952 
0953 /**
0954  *  dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
0955  *  @str: Case sensitive Name
0956  */
0957 int dmi_name_in_vendors(const char *str)
0958 {
0959     static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
0960     int i;
0961     for (i = 0; fields[i] != DMI_NONE; i++) {
0962         int f = fields[i];
0963         if (dmi_ident[f] && strstr(dmi_ident[f], str))
0964             return 1;
0965     }
0966     return 0;
0967 }
0968 EXPORT_SYMBOL(dmi_name_in_vendors);
0969 
0970 /**
0971  *  dmi_find_device - find onboard device by type/name
0972  *  @type: device type or %DMI_DEV_TYPE_ANY to match all device types
0973  *  @name: device name string or %NULL to match all
0974  *  @from: previous device found in search, or %NULL for new search.
0975  *
0976  *  Iterates through the list of known onboard devices. If a device is
0977  *  found with a matching @type and @name, a pointer to its device
0978  *  structure is returned.  Otherwise, %NULL is returned.
0979  *  A new search is initiated by passing %NULL as the @from argument.
0980  *  If @from is not %NULL, searches continue from next device.
0981  */
0982 const struct dmi_device *dmi_find_device(int type, const char *name,
0983                     const struct dmi_device *from)
0984 {
0985     const struct list_head *head = from ? &from->list : &dmi_devices;
0986     struct list_head *d;
0987 
0988     for (d = head->next; d != &dmi_devices; d = d->next) {
0989         const struct dmi_device *dev =
0990             list_entry(d, struct dmi_device, list);
0991 
0992         if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
0993             ((name == NULL) || (strcmp(dev->name, name) == 0)))
0994             return dev;
0995     }
0996 
0997     return NULL;
0998 }
0999 EXPORT_SYMBOL(dmi_find_device);
1000 
1001 /**
1002  *  dmi_get_date - parse a DMI date
1003  *  @field: data index (see enum dmi_field)
1004  *  @yearp: optional out parameter for the year
1005  *  @monthp: optional out parameter for the month
1006  *  @dayp: optional out parameter for the day
1007  *
1008  *  The date field is assumed to be in the form resembling
1009  *  [mm[/dd]]/yy[yy] and the result is stored in the out
1010  *  parameters any or all of which can be omitted.
1011  *
1012  *  If the field doesn't exist, all out parameters are set to zero
1013  *  and false is returned.  Otherwise, true is returned with any
1014  *  invalid part of date set to zero.
1015  *
1016  *  On return, year, month and day are guaranteed to be in the
1017  *  range of [0,9999], [0,12] and [0,31] respectively.
1018  */
1019 bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
1020 {
1021     int year = 0, month = 0, day = 0;
1022     bool exists;
1023     const char *s, *y;
1024     char *e;
1025 
1026     s = dmi_get_system_info(field);
1027     exists = s;
1028     if (!exists)
1029         goto out;
1030 
1031     /*
1032      * Determine year first.  We assume the date string resembles
1033      * mm/dd/yy[yy] but the original code extracted only the year
1034      * from the end.  Keep the behavior in the spirit of no
1035      * surprises.
1036      */
1037     y = strrchr(s, '/');
1038     if (!y)
1039         goto out;
1040 
1041     y++;
1042     year = simple_strtoul(y, &e, 10);
1043     if (y != e && year < 100) { /* 2-digit year */
1044         year += 1900;
1045         if (year < 1996)    /* no dates < spec 1.0 */
1046             year += 100;
1047     }
1048     if (year > 9999)        /* year should fit in %04d */
1049         year = 0;
1050 
1051     /* parse the mm and dd */
1052     month = simple_strtoul(s, &e, 10);
1053     if (s == e || *e != '/' || !month || month > 12) {
1054         month = 0;
1055         goto out;
1056     }
1057 
1058     s = e + 1;
1059     day = simple_strtoul(s, &e, 10);
1060     if (s == y || s == e || *e != '/' || day > 31)
1061         day = 0;
1062 out:
1063     if (yearp)
1064         *yearp = year;
1065     if (monthp)
1066         *monthp = month;
1067     if (dayp)
1068         *dayp = day;
1069     return exists;
1070 }
1071 EXPORT_SYMBOL(dmi_get_date);
1072 
1073 /**
1074  *  dmi_get_bios_year - get a year out of DMI_BIOS_DATE field
1075  *
1076  *  Returns year on success, -ENXIO if DMI is not selected,
1077  *  or a different negative error code if DMI field is not present
1078  *  or not parseable.
1079  */
1080 int dmi_get_bios_year(void)
1081 {
1082     bool exists;
1083     int year;
1084 
1085     exists = dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
1086     if (!exists)
1087         return -ENODATA;
1088 
1089     return year ? year : -ERANGE;
1090 }
1091 EXPORT_SYMBOL(dmi_get_bios_year);
1092 
1093 /**
1094  *  dmi_walk - Walk the DMI table and get called back for every record
1095  *  @decode: Callback function
1096  *  @private_data: Private data to be passed to the callback function
1097  *
1098  *  Returns 0 on success, -ENXIO if DMI is not selected or not present,
1099  *  or a different negative error code if DMI walking fails.
1100  */
1101 int dmi_walk(void (*decode)(const struct dmi_header *, void *),
1102          void *private_data)
1103 {
1104     u8 *buf;
1105 
1106     if (!dmi_available)
1107         return -ENXIO;
1108 
1109     buf = dmi_remap(dmi_base, dmi_len);
1110     if (buf == NULL)
1111         return -ENOMEM;
1112 
1113     dmi_decode_table(buf, decode, private_data);
1114 
1115     dmi_unmap(buf);
1116     return 0;
1117 }
1118 EXPORT_SYMBOL_GPL(dmi_walk);
1119 
1120 /**
1121  * dmi_match - compare a string to the dmi field (if exists)
1122  * @f: DMI field identifier
1123  * @str: string to compare the DMI field to
1124  *
1125  * Returns true if the requested field equals to the str (including NULL).
1126  */
1127 bool dmi_match(enum dmi_field f, const char *str)
1128 {
1129     const char *info = dmi_get_system_info(f);
1130 
1131     if (info == NULL || str == NULL)
1132         return info == str;
1133 
1134     return !strcmp(info, str);
1135 }
1136 EXPORT_SYMBOL_GPL(dmi_match);
1137 
1138 void dmi_memdev_name(u16 handle, const char **bank, const char **device)
1139 {
1140     int n;
1141 
1142     if (dmi_memdev == NULL)
1143         return;
1144 
1145     for (n = 0; n < dmi_memdev_nr; n++) {
1146         if (handle == dmi_memdev[n].handle) {
1147             *bank = dmi_memdev[n].bank;
1148             *device = dmi_memdev[n].device;
1149             break;
1150         }
1151     }
1152 }
1153 EXPORT_SYMBOL_GPL(dmi_memdev_name);
1154 
1155 u64 dmi_memdev_size(u16 handle)
1156 {
1157     int n;
1158 
1159     if (dmi_memdev) {
1160         for (n = 0; n < dmi_memdev_nr; n++) {
1161             if (handle == dmi_memdev[n].handle)
1162                 return dmi_memdev[n].size;
1163         }
1164     }
1165     return ~0ull;
1166 }
1167 EXPORT_SYMBOL_GPL(dmi_memdev_size);
1168 
1169 /**
1170  * dmi_memdev_type - get the memory type
1171  * @handle: DMI structure handle
1172  *
1173  * Return the DMI memory type of the module in the slot associated with the
1174  * given DMI handle, or 0x0 if no such DMI handle exists.
1175  */
1176 u8 dmi_memdev_type(u16 handle)
1177 {
1178     int n;
1179 
1180     if (dmi_memdev) {
1181         for (n = 0; n < dmi_memdev_nr; n++) {
1182             if (handle == dmi_memdev[n].handle)
1183                 return dmi_memdev[n].type;
1184         }
1185     }
1186     return 0x0; /* Not a valid value */
1187 }
1188 EXPORT_SYMBOL_GPL(dmi_memdev_type);
1189 
1190 /**
1191  *  dmi_memdev_handle - get the DMI handle of a memory slot
1192  *  @slot: slot number
1193  *
1194  *  Return the DMI handle associated with a given memory slot, or %0xFFFF
1195  *      if there is no such slot.
1196  */
1197 u16 dmi_memdev_handle(int slot)
1198 {
1199     if (dmi_memdev && slot >= 0 && slot < dmi_memdev_nr)
1200         return dmi_memdev[slot].handle;
1201 
1202     return 0xffff;  /* Not a valid value */
1203 }
1204 EXPORT_SYMBOL_GPL(dmi_memdev_handle);