From patchwork Thu May 21 07:15:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Kerr X-Patchwork-Id: 474847 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 6FC671400B7; Thu, 21 May 2015 17:15:24 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1YvKhP-0004kW-Ag; Thu, 21 May 2015 07:15:23 +0000 Received: from ozlabs.org ([103.22.144.67]) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1YvKhG-0004jK-L7 for fwts-devel@lists.ubuntu.com; Thu, 21 May 2015 07:15:14 +0000 Received: by ozlabs.org (Postfix, from userid 1023) id A053514012C; Thu, 21 May 2015 17:15:12 +1000 (AEST) MIME-Version: 1.0 Subject: [PATCH 3/3] devicetree/dt_sysinfo: Add device tree system information tests Message-Id: <1432192507.647628.90360957379.3.gpush@pablo> In-Reply-To: <1432192507.646890.191605150773.1.gpush@pablo> To: fwts-devel@lists.ubuntu.com From: Jeremy Kerr Date: Thu, 21 May 2015 15:15:07 +0800 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.14 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: fwts-devel-bounces@lists.ubuntu.com Check that we have some basic system identifier properties. Signed-off-by: Jeremy Kerr --- src/Makefile.am | 1 src/devicetree/dt_sysinfo/dt_sysinfo.c | 137 +++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/src/Makefile.am b/src/Makefile.am index 4a2622b..c0a8104 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -79,6 +79,7 @@ fwts_SOURCES = main.c \ cpu/msr/msr.c \ cpu/microcode/microcode.c \ devicetree/dtc/dtc.c \ + devicetree/dt_sysinfo/dt_sysinfo.c \ dmi/dmicheck/dmicheck.c \ hotkey/hotkey/hotkey.c \ hpet/hpet_check/hpet_check.c \ diff --git a/src/devicetree/dt_sysinfo/dt_sysinfo.c b/src/devicetree/dt_sysinfo/dt_sysinfo.c new file mode 100644 index 0000000..aa5514c --- /dev/null +++ b/src/devicetree/dt_sysinfo/dt_sysinfo.c @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2014 Jeremy Kerr + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + */ + +#define _GNU_SOURCE +#include +#include +#include + +#include "fwts.h" + +static const char *devicetree_path = "/proc/device-tree"; + +static int read_property(const char *name, char **buf, size_t *len) +{ + char *path; + int rc; + + rc = asprintf(&path, "%s/%s", devicetree_path, name); + assert(rc > 0); + + rc = fwts_file_open_and_read_binary(path, buf, len); + free(path); + + return rc; +} + +static int check_property_printable(fwts_framework *fw, const char *name, + char *buf, size_t len) +{ + bool printable = true; + unsigned int i; + int rc; + + /* we need at least one character plus a nul */ + if (len < 2) { + fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyShort", + "property %s is too short", name); + rc = FWTS_ERROR; + goto out; + } + + /* check all characters are printable */ + for (i = 0; i < len - 1; i++) { + printable = printable && isprint(buf[i]); + if (!printable) + break; + } + + if (!printable) { + fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyInvalid", + "property %s contains unprintable characters", + name); + rc = FWTS_ERROR; + goto out; + } + + /* check for a trailing nul */ + if (buf[len-1] != '\0') { + fwts_failed(fw, LOG_LEVEL_LOW, "DTPrintablePropertyNoNul", + "property %s isn't nul-terminated", name); + rc = FWTS_ERROR; + } + + rc = FWTS_OK; + +out: + return rc; +} + +static int dt_sysinfo_check_property(fwts_framework *fw, const char *name) +{ + size_t len; + char *buf; + int rc; + + rc = read_property(name, &buf, &len); + if (rc != FWTS_OK) { + fwts_failed(fw, LOG_LEVEL_LOW, "DTSyinfoPropertyMissing", + "property %s is missing", name); + return rc; + } + + rc = check_property_printable(fw, name, buf, len); + free(buf); + + if (rc == FWTS_OK) + fwts_passed(fw, "Sysinfo property %s is valid", name); + + return rc; +} + +static int dt_sysinfo_check_vendor(fwts_framework *fw) +{ + return dt_sysinfo_check_property(fw, "vendor"); +} + +static int dt_sysinfo_check_model(fwts_framework *fw) +{ + return dt_sysinfo_check_property(fw, "model"); +} + +static int dt_sysinfo_check_name(fwts_framework *fw) +{ + return dt_sysinfo_check_property(fw, "name"); +} + +static fwts_framework_minor_test dt_sysinfo_tests[] = { + { dt_sysinfo_check_name, "Check name property" }, + { dt_sysinfo_check_vendor, "Check vendor property" }, + { dt_sysinfo_check_model, "Check model property" }, + { NULL, NULL }, +}; + +static fwts_framework_ops dt_sysinfo_ops = { + .description = "Device tree system information test", + .minor_tests = dt_sysinfo_tests, +}; + +FWTS_REGISTER_FEATURES("dt_sysinfo", &dt_sysinfo_ops, FWTS_TEST_ANYTIME, + FWTS_FLAG_BATCH, FWTS_FW_FEATURE_DEVICETREE);