pdc_stable.c
字号:
for (i=0; i<size; i+=4) { if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result, sizeof(result)) != PDC_OK)) return -EIO; out += sprintf(out, "0x%.8x\n", result); } return out - buf;}/** * pdcs_auto_write - This function handles autoboot/search flag modifying. * @kset: An allocated and populated struct kset. We don't use it tho. * @buf: The input buffer to read from. * @count: The number of bytes to be read. * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag * * We will call this function to change the current autoboot flag. * We expect a precise syntax: * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On */static ssize_tpdcs_auto_write(struct kset *kset, const char *buf, size_t count, int knob){ struct pdcspath_entry *pathentry; unsigned char flags; char in[count+1], *temp; char c; if (!capable(CAP_SYS_ADMIN)) return -EACCES; if (!kset || !buf || !count) return -EINVAL; /* We'll use a local copy of buf */ memset(in, 0, count+1); strncpy(in, buf, count); /* Current flags are stored in primary boot path entry */ pathentry = &pdcspath_entry_primary; /* Be nice to the existing flag record */ read_lock(&pathentry->rw_lock); flags = pathentry->devpath.flags; read_unlock(&pathentry->rw_lock); DPRINTK("%s: flags before: 0x%X\n", __func__, flags); temp = in; while (*temp && isspace(*temp)) temp++; 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. * @kset: An allocated and populated struct kset. We don't use it tho. * @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 inline ssize_tpdcs_autoboot_write(struct kset *kset, const char *buf, size_t count){ return pdcs_auto_write(kset, buf, count, PF_AUTOBOOT);}/** * pdcs_autosearch_write - This function handles autosearch flag modifying. * @kset: An allocated and populated struct kset. We don't use it tho. * @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 inline ssize_tpdcs_autosearch_write(struct kset *kset, const char *buf, size_t count){ return pdcs_auto_write(kset, buf, count, PF_AUTOSEARCH);}/** * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input. * @kset: An allocated and populated struct kset. We don't use it tho. * @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_tpdcs_osdep1_write(struct kset *kset, const char *buf, size_t count){ u8 in[16]; if (!capable(CAP_SYS_ADMIN)) return -EACCES; if (!kset || !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. * @kset: An allocated and populated struct kset. We don't use it tho. * @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_tpdcs_osdep2_write(struct kset *kset, const char *buf, size_t count){ unsigned long size; unsigned short i; u8 in[4]; if (!capable(CAP_SYS_ADMIN)) return -EACCES; if (!kset || !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; i<count; i+=4) { memset(in, 0, 4); memcpy(in, buf+i, (count-i < 4) ? count-i : 4); if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in, sizeof(in)) != PDC_OK)) return -EIO; } return count;}/* The remaining attributes. */static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);static struct subsys_attribute *pdcs_subsys_attrs[] = { &pdcs_attr_size, &pdcs_attr_autoboot, &pdcs_attr_autosearch, &pdcs_attr_timer, &pdcs_attr_osid, &pdcs_attr_osdep1, &pdcs_attr_diagnostic, &pdcs_attr_fastsize, &pdcs_attr_osdep2, NULL,};static decl_subsys(paths, &ktype_pdcspath, NULL);static decl_subsys(stable, NULL, NULL);/** * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage. * * It creates kobjects corresponding to each path entry with nice sysfs * links to the real device. This is where the magic takes place: when * registering the subsystem attributes during module init, each kobject hereby * created will show in the sysfs tree as a folder containing files as defined * by path_subsys_attr[]. */static inline int __initpdcs_register_pathentries(void){ unsigned short i; struct pdcspath_entry *entry; int err; /* Initialize the entries rw_lock before anything else */ for (i = 0; (entry = pdcspath_entries[i]); i++) rwlock_init(&entry->rw_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; if ((err = kobject_set_name(&entry->kobj, "%s", entry->name))) return err; kobj_set_kset_s(entry, paths_subsys); if ((err = kobject_register(&entry->kobj))) 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); } return 0;}/** * pdcs_unregister_pathentries - Routine called when unregistering the module. */static inline voidpdcs_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_unregister(&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 __initpdc_stable_init(void){ struct subsys_attribute *attr; int i, 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 stable subsys within this driver */ if ((rc = firmware_register(&stable_subsys))) goto fail_firmreg; /* Don't forget the root entries */ for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++) if (attr->show) error = subsys_create_file(&stable_subsys, attr); /* register the paths subsys as a subsystem of stable subsys */ kobj_set_kset_s(&paths_subsys, stable_subsys); if ((rc = subsystem_register(&paths_subsys))) goto fail_subsysreg; /* now we create all "files" for the paths subsys */ if ((rc = pdcs_register_pathentries())) goto fail_pdcsreg; return rc; fail_pdcsreg: pdcs_unregister_pathentries(); subsystem_unregister(&paths_subsys); fail_subsysreg: firmware_unregister(&stable_subsys); fail_firmreg: printk(KERN_INFO PDCS_PREFIX " bailing out\n"); return rc;}static void __exitpdc_stable_exit(void){ pdcs_unregister_pathentries(); subsystem_unregister(&paths_subsys); firmware_unregister(&stable_subsys);}module_init(pdc_stable_init);module_exit(pdc_stable_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -