Home | History | Annotate | Line # | Download | only in linux
linux_dmi.c revision 1.1.34.1
      1  1.1.34.1  pgoyette /*	$NetBSD: linux_dmi.c,v 1.1.34.1 2018/09/06 06:56:37 pgoyette Exp $	*/
      2       1.1  riastrad 
      3       1.1  riastrad /*-
      4       1.1  riastrad  * Copyright (C) 2014 NONAKA Kimihiro <nonaka (at) netbsd.org>
      5       1.1  riastrad  * All rights reserved.
      6       1.1  riastrad  *
      7       1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8       1.1  riastrad  * modification, are permitted provided that the following conditions
      9       1.1  riastrad  * are met:
     10       1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11       1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12       1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15       1.1  riastrad  *
     16       1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1  riastrad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1  riastrad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1  riastrad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1  riastrad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1  riastrad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1  riastrad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1  riastrad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1  riastrad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1  riastrad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1  riastrad  */
     27       1.1  riastrad 
     28       1.1  riastrad #include <sys/cdefs.h>
     29  1.1.34.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: linux_dmi.c,v 1.1.34.1 2018/09/06 06:56:37 pgoyette Exp $");
     30       1.1  riastrad 
     31       1.1  riastrad #include <sys/param.h>
     32       1.1  riastrad #include <sys/types.h>
     33       1.1  riastrad #include <sys/pmf.h>
     34       1.1  riastrad 
     35       1.1  riastrad #include <linux/dmi.h>
     36       1.1  riastrad 
     37  1.1.34.1  pgoyette bool
     38  1.1.34.1  pgoyette dmi_match(enum dmi_field slot, const char *text)
     39  1.1.34.1  pgoyette {
     40  1.1.34.1  pgoyette 	const char *p = NULL;
     41  1.1.34.1  pgoyette 
     42  1.1.34.1  pgoyette 	switch (slot) {
     43  1.1.34.1  pgoyette 	case DMI_NONE:
     44  1.1.34.1  pgoyette 		aprint_error("%s: dmi_match on none makes no sense", __func__);
     45  1.1.34.1  pgoyette 		return false;
     46  1.1.34.1  pgoyette 	case DMI_BIOS_VENDOR:
     47  1.1.34.1  pgoyette 		p = pmf_get_platform("bios-vendor");
     48  1.1.34.1  pgoyette 		break;
     49  1.1.34.1  pgoyette 	case DMI_BIOS_VERSION:
     50  1.1.34.1  pgoyette 		p = pmf_get_platform("bios-version");
     51  1.1.34.1  pgoyette 		break;
     52  1.1.34.1  pgoyette 	case DMI_BIOS_DATE:
     53  1.1.34.1  pgoyette 		p = pmf_get_platform("bios-date");
     54  1.1.34.1  pgoyette 		break;
     55  1.1.34.1  pgoyette 	case DMI_SYS_VENDOR:
     56  1.1.34.1  pgoyette 		p = pmf_get_platform("system-vendor");
     57  1.1.34.1  pgoyette 		break;
     58  1.1.34.1  pgoyette 	case DMI_PRODUCT_NAME:
     59  1.1.34.1  pgoyette 		p = pmf_get_platform("system-product");
     60  1.1.34.1  pgoyette 		break;
     61  1.1.34.1  pgoyette 	case DMI_PRODUCT_VERSION:
     62  1.1.34.1  pgoyette 		p = pmf_get_platform("system-version");
     63  1.1.34.1  pgoyette 		break;
     64  1.1.34.1  pgoyette 	case DMI_PRODUCT_SERIAL:
     65  1.1.34.1  pgoyette 		p = pmf_get_platform("system-serial");
     66  1.1.34.1  pgoyette 		break;
     67  1.1.34.1  pgoyette 	case DMI_PRODUCT_UUID:
     68  1.1.34.1  pgoyette 		p = pmf_get_platform("system-uuid");
     69  1.1.34.1  pgoyette 		break;
     70  1.1.34.1  pgoyette 	case DMI_BOARD_VENDOR:
     71  1.1.34.1  pgoyette 		p = pmf_get_platform("board-vendor");
     72  1.1.34.1  pgoyette 		break;
     73  1.1.34.1  pgoyette 	case DMI_BOARD_NAME:
     74  1.1.34.1  pgoyette 		p = pmf_get_platform("board-product");
     75  1.1.34.1  pgoyette 		break;
     76  1.1.34.1  pgoyette 	case DMI_BOARD_VERSION:
     77  1.1.34.1  pgoyette 		p = pmf_get_platform("board-version");
     78  1.1.34.1  pgoyette 		break;
     79  1.1.34.1  pgoyette 	case DMI_BOARD_SERIAL:
     80  1.1.34.1  pgoyette 		p = pmf_get_platform("board-serial");
     81  1.1.34.1  pgoyette 		break;
     82  1.1.34.1  pgoyette 	case DMI_BOARD_ASSET_TAG:
     83  1.1.34.1  pgoyette 		p = pmf_get_platform("board-asset-tag");
     84  1.1.34.1  pgoyette 		break;
     85  1.1.34.1  pgoyette 	case DMI_CHASSIS_VENDOR:
     86  1.1.34.1  pgoyette 	case DMI_CHASSIS_TYPE:
     87  1.1.34.1  pgoyette 	case DMI_CHASSIS_VERSION:
     88  1.1.34.1  pgoyette 	case DMI_CHASSIS_SERIAL:
     89  1.1.34.1  pgoyette 	case DMI_CHASSIS_ASSET_TAG:
     90  1.1.34.1  pgoyette 		return false;
     91  1.1.34.1  pgoyette 	case DMI_STRING_MAX:
     92  1.1.34.1  pgoyette 	default:
     93  1.1.34.1  pgoyette 		aprint_error("%s: unknown DMI field(%d)\n", __func__, slot);
     94  1.1.34.1  pgoyette 		return false;
     95  1.1.34.1  pgoyette 	}
     96  1.1.34.1  pgoyette 	if (p == NULL || strcmp(p, text))
     97  1.1.34.1  pgoyette 		return false;
     98  1.1.34.1  pgoyette 
     99  1.1.34.1  pgoyette 	return true;
    100  1.1.34.1  pgoyette }
    101  1.1.34.1  pgoyette 
    102       1.1  riastrad static bool
    103       1.1  riastrad dmi_found(const struct dmi_system_id *dsi)
    104       1.1  riastrad {
    105  1.1.34.1  pgoyette 	int i;
    106       1.1  riastrad 
    107       1.1  riastrad 	for (i = 0; i < __arraycount(dsi->matches); i++) {
    108  1.1.34.1  pgoyette 		if (dsi->matches[i].slot == DMI_NONE)
    109       1.1  riastrad 			break;
    110  1.1.34.1  pgoyette 		if (!dmi_match(dsi->matches[i].slot, dsi->matches[i].substr))
    111       1.1  riastrad 			return false;
    112       1.1  riastrad 	}
    113       1.1  riastrad 	return true;
    114       1.1  riastrad }
    115       1.1  riastrad 
    116       1.1  riastrad int
    117       1.1  riastrad dmi_check_system(const struct dmi_system_id *sysid)
    118       1.1  riastrad {
    119       1.1  riastrad 	const struct dmi_system_id *dsi;
    120       1.1  riastrad 	int num = 0;
    121       1.1  riastrad 
    122       1.1  riastrad 	for (dsi = sysid; dsi->matches[0].slot != DMI_NONE; dsi++) {
    123       1.1  riastrad 		if (dmi_found(dsi)) {
    124       1.1  riastrad 			num++;
    125       1.1  riastrad 			if (dsi->callback && dsi->callback(dsi))
    126       1.1  riastrad 				break;
    127       1.1  riastrad 		}
    128       1.1  riastrad 	}
    129       1.1  riastrad 	return num;
    130       1.1  riastrad }
    131