/* * Interfaces to retrieve and set PDC Stable options (firmware) * * Copyright (C) 2005-2006 Thibaut VARENE * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, as * published by the Free Software Foundation. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * DEV NOTE: the PDC Procedures reference states that: * "A minimum of 96 bytes of Stable Storage is required. Providing more than * 96 bytes of Stable Storage is optional [...]. Failure to provide the * optional locations from 96 to 192 results in the loss of certain * functionality during boot." * * Since locations between 96 and 192 are the various paths, most (if not * all) PA-RISC machines should have them. Anyway, for safety reasons, the * following code can deal with just 96 bytes of Stable Storage, and all * sizes between 96 and 192 bytes (provided they are multiple of struct * device_path size, eg: 128, 160 and 192) to provide full information. * One last word: there's one path we can always count on: the primary path. * Anything above 224 bytes is used for 'osdep2' OS-dependent storage area. * * The first OS-dependent area should always be available. Obviously, this is * not true for the other one. Also bear in mind that reading/writing from/to * osdep2 is much more expensive than from/to osdep1. * NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first * 2 bytes of storage available right after OSID. That's a total of 4 bytes * sacrificed: -ETOOLAZY :P * * The current policy wrt file permissions is: * - write: root only * - read: (reading triggers PDC calls) ? root only : everyone * The rationale is that PDC calls could hog (DoS) the machine. * * TODO: * - timer/fastsize write calls */ #undef PDCS_DEBUG #ifdef PDCS_DEBUG #define DPRINTK(fmt, args...) printk(KERN_DEBUG fmt, ## args) #else #define DPRINTK(fmt, args...) #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PDCS_VERSION "0.30" #define PDCS_PREFIX "PDC Stable Storage" #define PDCS_ADDR_PPRI 0x00 #define PDCS_ADDR_OSID 0x40 #define PDCS_ADDR_OSD1 0x48 #define PDCS_ADDR_DIAG 0x58 #define PDCS_ADDR_FSIZ 0x5C #define PDCS_ADDR_PCON 0x60 #define PDCS_ADDR_PALT 0x80 #define PDCS_ADDR_PKBD 0xA0 #define PDCS_ADDR_OSD2 0xE0 MODULE_AUTHOR("Thibaut VARENE "); MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data"); MODULE_LICENSE("GPL"); MODULE_VERSION(PDCS_VERSION); /* holds Stable Storage size. Initialized once and for all, no lock needed */ static unsigned long pdcs_size __read_mostly; /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */ static u16 pdcs_osid __read_mostly; /* This struct defines what we need to deal with a parisc pdc path entry */ struct pdcspath_entry { rwlock_t rw_lock; /* to protect path entry access */ short ready; /* entry record is valid if != 0 */ unsigned long addr; /* entry address in stable storage */ char *name; /* entry name */ struct device_path devpath; /* device path in parisc representation */ struct device *dev; /* corresponding device */ struct kobject kobj; }; struct pdcspath_attribute { struct attribute attr; ssize_t (*show)(struct pdcspath_entry *entry, char *buf); ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count); }; #define PDCSPATH_ENTRY(_addr, _name) \ struct pdcspath_entry pdcspath_entry_##_name = { \ .ready = 0, \ .addr = _addr, \ .name = __stringify(_name), \ }; #define
/*
// Copyright (c) 2010-2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#ifndef _PROX_MALLOC_H_
#define _PROX_MALLOC_H_

#include <stddef.h>

void *prox_zmalloc(size_t size, int socket);
void prox_free(void *ptr);

#endif /* _PROX_MALLOC_H_ */
D, keyboard); /* An array containing all PDC paths we will deal with */ static struct pdcspath_entry *pdcspath_entries[] = { &pdcspath_entry_primary, &pdcspath_entry_alternative, &pdcspath_entry_console, &pdcspath_entry_keyboard, NULL, }; /* For more insight of what's going on here, refer to PDC Procedures doc, * Section PDC_STABLE */ /** * pdcs_size_read - Stable Storage size output. * @buf: The output buffer to write to. */ static ssize_t pdcs_size_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { char *out = buf; if (!buf) return -EINVAL; /* show the size of the stable storage */ out += sprintf(out, "%ld\n", pdcs_size); return out - buf; } /** * pdcs_auto_read - Stable Storage autoboot/search flag output. * @buf: The output buffer to write to. * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag */ static ssize_t pdcs_auto_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf, int knob) { char *out = buf; struct pdcspath_entry *pathentry; if (!buf) return -EINVAL; /* Current flags are stored in primary boot path entry */ pathentry = &pdcspath_entry_primary; read_lock(&pathentry->rw_lock); out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ? "On" : "Off"); read_unlock(&pathentry->rw_lock); return out - buf; } /** * pdcs_autoboot_read - Stable Storage autoboot flag output. * @buf: The output buffer to write to. */ static ssize_t pdcs_autoboot_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT); } /** * pdcs_autosearch_read - Stable Storage autoboot flag output. * @buf: The output buffer to write to. */ static ssize_t pdcs_autosearch_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH); } /** * pdcs_timer_read - Stable Storage timer count output (in seconds). * @buf: The output buffer to write to. * * The value of the timer field correponds to a number of seconds in powers of 2. */ static ssize_t pdcs_timer_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { char *out = buf; struct pdcspath_entry *pathentry; if (!buf) return -EINVAL; /* Current flags are stored in primary boot path entry */ pathentry = &pdcspath_entry_primary; /* print the timer value in seconds */ read_lock(&pathentry->rw_lock); out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ? (1 << (pathentry->devpath.flags & PF_TIMER)) : 0); read_unlock(&pathentry->rw_lock); return out - buf; } /** * pdcs_osid_read - Stable Storage OS ID register output. * @buf: The output buffer to write to. */ static ssize_t pdcs_osid_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { char *out = buf; if (!buf) return -EINVAL; out += sprintf(out, "%s dependent data (0x%.4x)\n", os_id_to_string(pdcs_osid), pdcs_osid); return out - buf; } /** * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output. * @buf: The output buffer to write to. * * This can hold 16 bytes of OS-Dependent data. */ static ssize_t pdcs_osdep1_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { char *out = buf; u32 result[4]; if (!buf) return -EINVAL; if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK) return -EIO; out += sprintf(out, "0x%.8x\n", result[0]); out += sprintf(out, "0x%.8x\n", result[1]); out += sprintf(out, "0x%.8x\n", result[2]); out += sprintf(out, "0x%.8x\n", result[3]); return out - buf; } /** * pdcs_diagnostic_read - Stable Storage Diagnostic register output. * @buf: The output buffer to write to. * * I have NFC how to interpret the content of that register ;-). */ static ssize_t pdcs_diagnostic_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { char *out = buf; u32 result; if (!buf) return -EINVAL; /* get diagnostic */ if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK) return -EIO; out += sprintf(out, "0x%.4x\n", (result >> 16)); return out - buf; } /** * pdcs_fastsize_read - Stable Storage FastSize register output. * @buf: The output buffer to write to. * * This register holds the amount of system RAM to be tested during boot sequence. */ static ssize_t pdcs_fastsize_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { char *out = buf; u32 result; if (!buf) return -EINVAL; /* get fast-size */ if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK) return -EIO; if ((result & 0x0F) < 0x0E) out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256); else out += sprintf(out, "All"); out += sprintf(out, "\n"); return out - buf; } /** * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output. * @buf: The output buffer to write to. * * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available. */ static ssize_t pdcs_osdep2_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { char *out = buf; unsigned long size; unsigned short i; u32 result; if (unlikely(pdcs_size <= 224)) return -ENODATA; size = pdcs_size - 224; if (!buf) return -EINVAL; for (i=0; irw_lock); flags = pathentry->devpath.flags; read_unlock(&pathentry->rw_lock); DPRINTK("%s: flags before: 0x%X\n", __func__, flags); temp = skip_spaces(in); c = *temp++ - '0'; if ((c != 0) && (c != 1)) goto parse_error; if (c == 0) flags &= ~knob; else flags |= knob; DPRINTK("%s: flags after: 0x%X\n", __func__, flags); /* So far so good, let's get in deep */ write_lock(&pathentry->rw_lock); /* Change the path entry flags first */ pathentry->devpath.flags = flags; /* Now, dive in. Write back to the hardware */ pdcspath_store(pathentry); write_unlock(&pathentry->rw_lock); printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n", (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch", (flags & knob) ? "On" : "Off"); return count; parse_error: printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__); return -EINVAL; } /** * pdcs_autoboot_write - This function handles autoboot flag modifying. * @buf: The input buffer to read from. * @count: The number of bytes to be read. * * We will call this function to change the current boot flags. * We expect a precise syntax: * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On */ static ssize_t pdcs_autoboot_write(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT); } /** * pdcs_autosearch_write - This function handles autosearch flag modifying. * @buf: The input buffer to read from. * @count: The number of bytes to be read. * * We will call this function to change the current boot flags. * We expect a precise syntax: * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On */ static ssize_t pdcs_autosearch_write(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH); } /** * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input. * @buf: The input buffer to read from. * @count: The number of bytes to be read. * * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte * write approach. It's up to userspace to deal with it when constructing * its input buffer. */ static ssize_t pdcs_osdep1_write(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { u8 in[16]; if (!capable(CAP_SYS_ADMIN)) return -EACCES; if (!buf || !count) return -EINVAL; if (unlikely(pdcs_osid != OS_ID_LINUX)) return -EPERM; if (count > 16) return -EMSGSIZE; /* We'll use a local copy of buf */ memset(in, 0, 16); memcpy(in, buf, count); if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK) return -EIO; return count; } /** * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input. * @buf: The input buffer to read from. * @count: The number of bytes to be read. * * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a * byte-by-byte write approach. It's up to userspace to deal with it when * constructing its input buffer. */ static ssize_t pdcs_osdep2_write(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { unsigned long size; unsigned short i; u8 in[4]; if (!capable(CAP_SYS_ADMIN)) return -EACCES; if (!buf || !count) return -EINVAL; if (unlikely(pdcs_size <= 224)) return -ENOSYS; if (unlikely(pdcs_osid != OS_ID_LINUX)) return -EPERM; size = pdcs_size - 224; if (count > size) return -EMSGSIZE; /* We'll use a local copy of buf */ for (i=0; irw_lock); for (i = 0; (entry = pdcspath_entries[i]); i++) { write_lock(&entry->rw_lock); err = pdcspath_fetch(entry); write_unlock(&entry->rw_lock); if (err < 0) continue; entry->kobj.kset = paths_kset; err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL, "%s", entry->name); if (err) return err; /* kobject is now registered */ write_lock(&entry->rw_lock); entry->ready = 2; /* Add a nice symlink to the real device */ if (entry->dev) { err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device"); WARN_ON(err); } write_unlock(&entry->rw_lock); kobject_uevent(&entry->kobj, KOBJ_ADD); } return 0; } /** * pdcs_unregister_pathentries - Routine called when unregistering the module. */ static inline void pdcs_unregister_pathentries(void) { unsigned short i; struct pdcspath_entry *entry; for (i = 0; (entry = pdcspath_entries[i]); i++) { read_lock(&entry->rw_lock); if (entry->ready >= 2) kobject_put(&entry->kobj); read_unlock(&entry->rw_lock); } } /* * For now we register the stable subsystem with the firmware subsystem * and the paths subsystem with the stable subsystem */ static int __init pdc_stable_init(void) { int rc = 0, error = 0; u32 result; /* find the size of the stable storage */ if (pdc_stable_get_size(&pdcs_size) != PDC_OK) return -ENODEV; /* make sure we have enough data */ if (pdcs_size < 96) return -ENODATA; printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION); /* get OSID */ if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK) return -EIO; /* the actual result is 16 bits away */ pdcs_osid = (u16)(result >> 16); /* For now we'll register the directory at /sys/firmware/stable */ stable_kobj = kobject_create_and_add("stable", firmware_kobj); if (!stable_kobj) { rc = -ENOMEM; goto fail_firmreg; } /* Don't forget the root entries */ error = sysfs_create_group(stable_kobj, &pdcs_attr_group); /* register the paths kset as a child of the stable kset */ paths_kset = kset_create_and_add("paths", NULL, stable_kobj); if (!paths_kset) { rc = -ENOMEM; goto fail_ksetreg; } /* now we create all "files" for the paths kset */ if ((rc = pdcs_register_pathentries())) goto fail_pdcsreg; return rc; fail_pdcsreg: pdcs_unregister_pathentries(); kset_unregister(paths_kset); fail_ksetreg: kobject_put(stable_kobj); fail_firmreg: printk(KERN_INFO PDCS_PREFIX " bailing out\n"); return rc; } static void __exit pdc_stable_exit(void) { pdcs_unregister_pathentries(); kset_unregister(paths_kset); kobject_put(stable_kobj); } module_init(pdc_stable_init); module_exit(pdc_stable_exit);