efivars.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*
  2. * EFI Variables - efivars.c
  3. *
  4. * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
  5. * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
  6. *
  7. * This code takes all variables accessible from EFI runtime and
  8. * exports them via sysfs
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Changelog:
  25. *
  26. * 17 May 2004 - Matt Domsch <Matt_Domsch@dell.com>
  27. * remove check for efi_enabled in exit
  28. * add MODULE_VERSION
  29. *
  30. * 26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com>
  31. * minor bug fixes
  32. *
  33. * 21 Apr 2004 - Matt Tolentino <matthew.e.tolentino@intel.com)
  34. * converted driver to export variable information via sysfs
  35. * and moved to drivers/firmware directory
  36. * bumped revision number to v0.07 to reflect conversion & move
  37. *
  38. * 10 Dec 2002 - Matt Domsch <Matt_Domsch@dell.com>
  39. * fix locking per Peter Chubb's findings
  40. *
  41. * 25 Mar 2002 - Matt Domsch <Matt_Domsch@dell.com>
  42. * move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_unparse()
  43. *
  44. * 12 Feb 2002 - Matt Domsch <Matt_Domsch@dell.com>
  45. * use list_for_each_safe when deleting vars.
  46. * remove ifdef CONFIG_SMP around include <linux/smp.h>
  47. * v0.04 release to linux-ia64@linuxia64.org
  48. *
  49. * 20 April 2001 - Matt Domsch <Matt_Domsch@dell.com>
  50. * Moved vars from /proc/efi to /proc/efi/vars, and made
  51. * efi.c own the /proc/efi directory.
  52. * v0.03 release to linux-ia64@linuxia64.org
  53. *
  54. * 26 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
  55. * At the request of Stephane, moved ownership of /proc/efi
  56. * to efi.c, and now efivars lives under /proc/efi/vars.
  57. *
  58. * 12 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
  59. * Feedback received from Stephane Eranian incorporated.
  60. * efivar_write() checks copy_from_user() return value.
  61. * efivar_read/write() returns proper errno.
  62. * v0.02 release to linux-ia64@linuxia64.org
  63. *
  64. * 26 February 2001 - Matt Domsch <Matt_Domsch@dell.com>
  65. * v0.01 release to linux-ia64@linuxia64.org
  66. */
  67. #include <linux/capability.h>
  68. #include <linux/types.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/mm.h>
  72. #include <linux/module.h>
  73. #include <linux/string.h>
  74. #include <linux/smp.h>
  75. #include <linux/efi.h>
  76. #include <linux/sysfs.h>
  77. #include <linux/kobject.h>
  78. #include <linux/device.h>
  79. #include <linux/slab.h>
  80. #include <linux/pstore.h>
  81. #include <linux/fs.h>
  82. #include <linux/ramfs.h>
  83. #include <linux/pagemap.h>
  84. #include <asm/uaccess.h>
  85. #define EFIVARS_VERSION "0.08"
  86. #define EFIVARS_DATE "2004-May-17"
  87. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  88. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  89. MODULE_LICENSE("GPL");
  90. MODULE_VERSION(EFIVARS_VERSION);
  91. #define DUMP_NAME_LEN 52
  92. #define GUID_LEN 37
  93. /*
  94. * The maximum size of VariableName + Data = 1024
  95. * Therefore, it's reasonable to save that much
  96. * space in each part of the structure,
  97. * and we use a page for reading/writing.
  98. */
  99. struct efi_variable {
  100. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  101. efi_guid_t VendorGuid;
  102. unsigned long DataSize;
  103. __u8 Data[1024];
  104. efi_status_t Status;
  105. __u32 Attributes;
  106. } __attribute__((packed));
  107. struct efivar_entry {
  108. struct efivars *efivars;
  109. struct efi_variable var;
  110. struct list_head list;
  111. struct kobject kobj;
  112. };
  113. struct efivar_attribute {
  114. struct attribute attr;
  115. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  116. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  117. };
  118. static struct efivars __efivars;
  119. static struct efivar_operations ops;
  120. #define PSTORE_EFI_ATTRIBUTES \
  121. (EFI_VARIABLE_NON_VOLATILE | \
  122. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  123. EFI_VARIABLE_RUNTIME_ACCESS)
  124. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  125. struct efivar_attribute efivar_attr_##_name = { \
  126. .attr = {.name = __stringify(_name), .mode = _mode}, \
  127. .show = _show, \
  128. .store = _store, \
  129. };
  130. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  131. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  132. /*
  133. * Prototype for sysfs creation function
  134. */
  135. static int
  136. efivar_create_sysfs_entry(struct efivars *efivars,
  137. unsigned long variable_name_size,
  138. efi_char16_t *variable_name,
  139. efi_guid_t *vendor_guid);
  140. /* Return the number of unicode characters in data */
  141. static unsigned long
  142. utf16_strnlen(efi_char16_t *s, size_t maxlength)
  143. {
  144. unsigned long length = 0;
  145. while (*s++ != 0 && length < maxlength)
  146. length++;
  147. return length;
  148. }
  149. static inline unsigned long
  150. utf16_strlen(efi_char16_t *s)
  151. {
  152. return utf16_strnlen(s, ~0UL);
  153. }
  154. /*
  155. * Return the number of bytes is the length of this string
  156. * Note: this is NOT the same as the number of unicode characters
  157. */
  158. static inline unsigned long
  159. utf16_strsize(efi_char16_t *data, unsigned long maxlength)
  160. {
  161. return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
  162. }
  163. static inline int
  164. utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
  165. {
  166. while (1) {
  167. if (len == 0)
  168. return 0;
  169. if (*a < *b)
  170. return -1;
  171. if (*a > *b)
  172. return 1;
  173. if (*a == 0) /* implies *b == 0 */
  174. return 0;
  175. a++;
  176. b++;
  177. len--;
  178. }
  179. }
  180. static bool
  181. validate_device_path(struct efi_variable *var, int match, u8 *buffer,
  182. unsigned long len)
  183. {
  184. struct efi_generic_dev_path *node;
  185. int offset = 0;
  186. node = (struct efi_generic_dev_path *)buffer;
  187. if (len < sizeof(*node))
  188. return false;
  189. while (offset <= len - sizeof(*node) &&
  190. node->length >= sizeof(*node) &&
  191. node->length <= len - offset) {
  192. offset += node->length;
  193. if ((node->type == EFI_DEV_END_PATH ||
  194. node->type == EFI_DEV_END_PATH2) &&
  195. node->sub_type == EFI_DEV_END_ENTIRE)
  196. return true;
  197. node = (struct efi_generic_dev_path *)(buffer + offset);
  198. }
  199. /*
  200. * If we're here then either node->length pointed past the end
  201. * of the buffer or we reached the end of the buffer without
  202. * finding a device path end node.
  203. */
  204. return false;
  205. }
  206. static bool
  207. validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
  208. unsigned long len)
  209. {
  210. /* An array of 16-bit integers */
  211. if ((len % 2) != 0)
  212. return false;
  213. return true;
  214. }
  215. static bool
  216. validate_load_option(struct efi_variable *var, int match, u8 *buffer,
  217. unsigned long len)
  218. {
  219. u16 filepathlength;
  220. int i, desclength = 0, namelen;
  221. namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
  222. /* Either "Boot" or "Driver" followed by four digits of hex */
  223. for (i = match; i < match+4; i++) {
  224. if (var->VariableName[i] > 127 ||
  225. hex_to_bin(var->VariableName[i] & 0xff) < 0)
  226. return true;
  227. }
  228. /* Reject it if there's 4 digits of hex and then further content */
  229. if (namelen > match + 4)
  230. return false;
  231. /* A valid entry must be at least 8 bytes */
  232. if (len < 8)
  233. return false;
  234. filepathlength = buffer[4] | buffer[5] << 8;
  235. /*
  236. * There's no stored length for the description, so it has to be
  237. * found by hand
  238. */
  239. desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  240. /* Each boot entry must have a descriptor */
  241. if (!desclength)
  242. return false;
  243. /*
  244. * If the sum of the length of the description, the claimed filepath
  245. * length and the original header are greater than the length of the
  246. * variable, it's malformed
  247. */
  248. if ((desclength + filepathlength + 6) > len)
  249. return false;
  250. /*
  251. * And, finally, check the filepath
  252. */
  253. return validate_device_path(var, match, buffer + desclength + 6,
  254. filepathlength);
  255. }
  256. static bool
  257. validate_uint16(struct efi_variable *var, int match, u8 *buffer,
  258. unsigned long len)
  259. {
  260. /* A single 16-bit integer */
  261. if (len != 2)
  262. return false;
  263. return true;
  264. }
  265. static bool
  266. validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
  267. unsigned long len)
  268. {
  269. int i;
  270. for (i = 0; i < len; i++) {
  271. if (buffer[i] > 127)
  272. return false;
  273. if (buffer[i] == 0)
  274. return true;
  275. }
  276. return false;
  277. }
  278. struct variable_validate {
  279. char *name;
  280. bool (*validate)(struct efi_variable *var, int match, u8 *data,
  281. unsigned long len);
  282. };
  283. static const struct variable_validate variable_validate[] = {
  284. { "BootNext", validate_uint16 },
  285. { "BootOrder", validate_boot_order },
  286. { "DriverOrder", validate_boot_order },
  287. { "Boot*", validate_load_option },
  288. { "Driver*", validate_load_option },
  289. { "ConIn", validate_device_path },
  290. { "ConInDev", validate_device_path },
  291. { "ConOut", validate_device_path },
  292. { "ConOutDev", validate_device_path },
  293. { "ErrOut", validate_device_path },
  294. { "ErrOutDev", validate_device_path },
  295. { "Timeout", validate_uint16 },
  296. { "Lang", validate_ascii_string },
  297. { "PlatformLang", validate_ascii_string },
  298. { "", NULL },
  299. };
  300. static bool
  301. validate_var(struct efi_variable *var, u8 *data, unsigned long len)
  302. {
  303. int i;
  304. u16 *unicode_name = var->VariableName;
  305. for (i = 0; variable_validate[i].validate != NULL; i++) {
  306. const char *name = variable_validate[i].name;
  307. int match;
  308. for (match = 0; ; match++) {
  309. char c = name[match];
  310. u16 u = unicode_name[match];
  311. /* All special variables are plain ascii */
  312. if (u > 127)
  313. return true;
  314. /* Wildcard in the matching name means we've matched */
  315. if (c == '*')
  316. return variable_validate[i].validate(var,
  317. match, data, len);
  318. /* Case sensitive match */
  319. if (c != u)
  320. break;
  321. /* Reached the end of the string while matching */
  322. if (!c)
  323. return variable_validate[i].validate(var,
  324. match, data, len);
  325. }
  326. }
  327. return true;
  328. }
  329. static efi_status_t
  330. get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
  331. {
  332. efi_status_t status;
  333. var->DataSize = 1024;
  334. status = efivars->ops->get_variable(var->VariableName,
  335. &var->VendorGuid,
  336. &var->Attributes,
  337. &var->DataSize,
  338. var->Data);
  339. return status;
  340. }
  341. static efi_status_t
  342. get_var_data(struct efivars *efivars, struct efi_variable *var)
  343. {
  344. efi_status_t status;
  345. spin_lock(&efivars->lock);
  346. status = get_var_data_locked(efivars, var);
  347. spin_unlock(&efivars->lock);
  348. if (status != EFI_SUCCESS) {
  349. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  350. status);
  351. }
  352. return status;
  353. }
  354. static ssize_t
  355. efivar_guid_read(struct efivar_entry *entry, char *buf)
  356. {
  357. struct efi_variable *var = &entry->var;
  358. char *str = buf;
  359. if (!entry || !buf)
  360. return 0;
  361. efi_guid_unparse(&var->VendorGuid, str);
  362. str += strlen(str);
  363. str += sprintf(str, "\n");
  364. return str - buf;
  365. }
  366. static ssize_t
  367. efivar_attr_read(struct efivar_entry *entry, char *buf)
  368. {
  369. struct efi_variable *var = &entry->var;
  370. char *str = buf;
  371. efi_status_t status;
  372. if (!entry || !buf)
  373. return -EINVAL;
  374. status = get_var_data(entry->efivars, var);
  375. if (status != EFI_SUCCESS)
  376. return -EIO;
  377. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  378. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  379. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  380. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  381. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  382. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  383. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  384. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  385. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  386. str += sprintf(str,
  387. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  388. if (var->Attributes &
  389. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  390. str += sprintf(str,
  391. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  392. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  393. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  394. return str - buf;
  395. }
  396. static ssize_t
  397. efivar_size_read(struct efivar_entry *entry, char *buf)
  398. {
  399. struct efi_variable *var = &entry->var;
  400. char *str = buf;
  401. efi_status_t status;
  402. if (!entry || !buf)
  403. return -EINVAL;
  404. status = get_var_data(entry->efivars, var);
  405. if (status != EFI_SUCCESS)
  406. return -EIO;
  407. str += sprintf(str, "0x%lx\n", var->DataSize);
  408. return str - buf;
  409. }
  410. static ssize_t
  411. efivar_data_read(struct efivar_entry *entry, char *buf)
  412. {
  413. struct efi_variable *var = &entry->var;
  414. efi_status_t status;
  415. if (!entry || !buf)
  416. return -EINVAL;
  417. status = get_var_data(entry->efivars, var);
  418. if (status != EFI_SUCCESS)
  419. return -EIO;
  420. memcpy(buf, var->Data, var->DataSize);
  421. return var->DataSize;
  422. }
  423. /*
  424. * We allow each variable to be edited via rewriting the
  425. * entire efi variable structure.
  426. */
  427. static ssize_t
  428. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  429. {
  430. struct efi_variable *new_var, *var = &entry->var;
  431. struct efivars *efivars = entry->efivars;
  432. efi_status_t status = EFI_NOT_FOUND;
  433. if (count != sizeof(struct efi_variable))
  434. return -EINVAL;
  435. new_var = (struct efi_variable *)buf;
  436. /*
  437. * If only updating the variable data, then the name
  438. * and guid should remain the same
  439. */
  440. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  441. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  442. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  443. return -EINVAL;
  444. }
  445. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  446. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  447. return -EINVAL;
  448. }
  449. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  450. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  451. printk(KERN_ERR "efivars: Malformed variable content\n");
  452. return -EINVAL;
  453. }
  454. spin_lock(&efivars->lock);
  455. status = efivars->ops->set_variable(new_var->VariableName,
  456. &new_var->VendorGuid,
  457. new_var->Attributes,
  458. new_var->DataSize,
  459. new_var->Data);
  460. spin_unlock(&efivars->lock);
  461. if (status != EFI_SUCCESS) {
  462. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  463. status);
  464. return -EIO;
  465. }
  466. memcpy(&entry->var, new_var, count);
  467. return count;
  468. }
  469. static ssize_t
  470. efivar_show_raw(struct efivar_entry *entry, char *buf)
  471. {
  472. struct efi_variable *var = &entry->var;
  473. efi_status_t status;
  474. if (!entry || !buf)
  475. return 0;
  476. status = get_var_data(entry->efivars, var);
  477. if (status != EFI_SUCCESS)
  478. return -EIO;
  479. memcpy(buf, var, sizeof(*var));
  480. return sizeof(*var);
  481. }
  482. /*
  483. * Generic read/write functions that call the specific functions of
  484. * the attributes...
  485. */
  486. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  487. char *buf)
  488. {
  489. struct efivar_entry *var = to_efivar_entry(kobj);
  490. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  491. ssize_t ret = -EIO;
  492. if (!capable(CAP_SYS_ADMIN))
  493. return -EACCES;
  494. if (efivar_attr->show) {
  495. ret = efivar_attr->show(var, buf);
  496. }
  497. return ret;
  498. }
  499. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  500. const char *buf, size_t count)
  501. {
  502. struct efivar_entry *var = to_efivar_entry(kobj);
  503. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  504. ssize_t ret = -EIO;
  505. if (!capable(CAP_SYS_ADMIN))
  506. return -EACCES;
  507. if (efivar_attr->store)
  508. ret = efivar_attr->store(var, buf, count);
  509. return ret;
  510. }
  511. static const struct sysfs_ops efivar_attr_ops = {
  512. .show = efivar_attr_show,
  513. .store = efivar_attr_store,
  514. };
  515. static void efivar_release(struct kobject *kobj)
  516. {
  517. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  518. kfree(var);
  519. }
  520. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  521. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  522. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  523. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  524. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  525. static struct attribute *def_attrs[] = {
  526. &efivar_attr_guid.attr,
  527. &efivar_attr_size.attr,
  528. &efivar_attr_attributes.attr,
  529. &efivar_attr_data.attr,
  530. &efivar_attr_raw_var.attr,
  531. NULL,
  532. };
  533. static struct kobj_type efivar_ktype = {
  534. .release = efivar_release,
  535. .sysfs_ops = &efivar_attr_ops,
  536. .default_attrs = def_attrs,
  537. };
  538. static inline void
  539. efivar_unregister(struct efivar_entry *var)
  540. {
  541. kobject_put(&var->kobj);
  542. }
  543. static int efivarfs_file_open(struct inode *inode, struct file *file)
  544. {
  545. file->private_data = inode->i_private;
  546. return 0;
  547. }
  548. static ssize_t efivarfs_file_write(struct file *file,
  549. const char __user *userbuf, size_t count, loff_t *ppos)
  550. {
  551. struct efivar_entry *var = file->private_data;
  552. struct efivars *efivars;
  553. efi_status_t status;
  554. void *data;
  555. u32 attributes;
  556. struct inode *inode = file->f_mapping->host;
  557. int datasize = count - sizeof(attributes);
  558. if (count < sizeof(attributes))
  559. return -EINVAL;
  560. data = kmalloc(datasize, GFP_KERNEL);
  561. if (!data)
  562. return -ENOMEM;
  563. efivars = var->efivars;
  564. if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
  565. count = -EFAULT;
  566. goto out;
  567. }
  568. if (attributes & ~(EFI_VARIABLE_MASK)) {
  569. count = -EINVAL;
  570. goto out;
  571. }
  572. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  573. count = -EFAULT;
  574. goto out;
  575. }
  576. if (validate_var(&var->var, data, datasize) == false) {
  577. count = -EINVAL;
  578. goto out;
  579. }
  580. status = efivars->ops->set_variable(var->var.VariableName,
  581. &var->var.VendorGuid,
  582. attributes, datasize,
  583. data);
  584. switch (status) {
  585. case EFI_SUCCESS:
  586. mutex_lock(&inode->i_mutex);
  587. i_size_write(inode, count);
  588. mutex_unlock(&inode->i_mutex);
  589. break;
  590. case EFI_INVALID_PARAMETER:
  591. count = -EINVAL;
  592. break;
  593. case EFI_OUT_OF_RESOURCES:
  594. count = -ENOSPC;
  595. break;
  596. case EFI_DEVICE_ERROR:
  597. count = -EIO;
  598. break;
  599. case EFI_WRITE_PROTECTED:
  600. count = -EROFS;
  601. break;
  602. case EFI_SECURITY_VIOLATION:
  603. count = -EACCES;
  604. break;
  605. case EFI_NOT_FOUND:
  606. count = -ENOENT;
  607. break;
  608. default:
  609. count = -EINVAL;
  610. break;
  611. }
  612. out:
  613. kfree(data);
  614. return count;
  615. }
  616. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  617. size_t count, loff_t *ppos)
  618. {
  619. struct efivar_entry *var = file->private_data;
  620. struct efivars *efivars = var->efivars;
  621. efi_status_t status;
  622. unsigned long datasize = 0;
  623. u32 attributes;
  624. void *data;
  625. ssize_t size;
  626. status = efivars->ops->get_variable(var->var.VariableName,
  627. &var->var.VendorGuid,
  628. &attributes, &datasize, NULL);
  629. if (status != EFI_BUFFER_TOO_SMALL)
  630. return 0;
  631. data = kmalloc(datasize + 4, GFP_KERNEL);
  632. if (!data)
  633. return 0;
  634. status = efivars->ops->get_variable(var->var.VariableName,
  635. &var->var.VendorGuid,
  636. &attributes, &datasize,
  637. (data + 4));
  638. if (status != EFI_SUCCESS)
  639. return 0;
  640. memcpy(data, &attributes, 4);
  641. size = simple_read_from_buffer(userbuf, count, ppos,
  642. data, datasize + 4);
  643. kfree(data);
  644. return size;
  645. }
  646. static void efivarfs_evict_inode(struct inode *inode)
  647. {
  648. clear_inode(inode);
  649. }
  650. static const struct super_operations efivarfs_ops = {
  651. .statfs = simple_statfs,
  652. .drop_inode = generic_delete_inode,
  653. .evict_inode = efivarfs_evict_inode,
  654. .show_options = generic_show_options,
  655. };
  656. static struct super_block *efivarfs_sb;
  657. static const struct inode_operations efivarfs_dir_inode_operations;
  658. static const struct file_operations efivarfs_file_operations = {
  659. .open = efivarfs_file_open,
  660. .read = efivarfs_file_read,
  661. .write = efivarfs_file_write,
  662. .llseek = no_llseek,
  663. };
  664. static struct inode *efivarfs_get_inode(struct super_block *sb,
  665. const struct inode *dir, int mode, dev_t dev)
  666. {
  667. struct inode *inode = new_inode(sb);
  668. if (inode) {
  669. inode->i_ino = get_next_ino();
  670. inode->i_uid = inode->i_gid = 0;
  671. inode->i_mode = mode;
  672. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  673. switch (mode & S_IFMT) {
  674. case S_IFREG:
  675. inode->i_fop = &efivarfs_file_operations;
  676. break;
  677. case S_IFDIR:
  678. inode->i_op = &efivarfs_dir_inode_operations;
  679. inode->i_fop = &simple_dir_operations;
  680. inc_nlink(inode);
  681. break;
  682. }
  683. }
  684. return inode;
  685. }
  686. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  687. {
  688. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  689. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  690. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  691. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  692. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  693. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  694. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  695. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  696. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  697. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  698. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  699. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  700. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  701. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  702. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  703. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  704. }
  705. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  706. umode_t mode, bool excl)
  707. {
  708. struct inode *inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  709. struct efivars *efivars = &__efivars;
  710. struct efivar_entry *var;
  711. int namelen, i = 0, err = 0;
  712. if (dentry->d_name.len < 38)
  713. return -EINVAL;
  714. if (!inode)
  715. return -ENOSPC;
  716. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  717. if (!var)
  718. return -ENOMEM;
  719. namelen = dentry->d_name.len - GUID_LEN;
  720. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  721. &var->var.VendorGuid);
  722. for (i = 0; i < namelen; i++)
  723. var->var.VariableName[i] = dentry->d_name.name[i];
  724. var->var.VariableName[i] = '\0';
  725. inode->i_private = var;
  726. var->efivars = efivars;
  727. var->kobj.kset = efivars->kset;
  728. err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
  729. dentry->d_name.name);
  730. if (err)
  731. goto out;
  732. kobject_uevent(&var->kobj, KOBJ_ADD);
  733. spin_lock(&efivars->lock);
  734. list_add(&var->list, &efivars->list);
  735. spin_unlock(&efivars->lock);
  736. d_instantiate(dentry, inode);
  737. dget(dentry);
  738. out:
  739. if (err)
  740. kfree(var);
  741. return err;
  742. }
  743. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  744. {
  745. struct efivar_entry *var = dentry->d_inode->i_private;
  746. struct efivars *efivars = var->efivars;
  747. efi_status_t status;
  748. spin_lock(&efivars->lock);
  749. status = efivars->ops->set_variable(var->var.VariableName,
  750. &var->var.VendorGuid,
  751. 0, 0, NULL);
  752. if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
  753. list_del(&var->list);
  754. spin_unlock(&efivars->lock);
  755. efivar_unregister(var);
  756. drop_nlink(dir);
  757. dput(dentry);
  758. return 0;
  759. }
  760. spin_unlock(&efivars->lock);
  761. return -EINVAL;
  762. };
  763. int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  764. {
  765. struct inode *inode = NULL;
  766. struct dentry *root;
  767. struct efivar_entry *entry, *n;
  768. struct efivars *efivars = &__efivars;
  769. int err;
  770. efivarfs_sb = sb;
  771. sb->s_maxbytes = MAX_LFS_FILESIZE;
  772. sb->s_blocksize = PAGE_CACHE_SIZE;
  773. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  774. sb->s_magic = PSTOREFS_MAGIC;
  775. sb->s_op = &efivarfs_ops;
  776. sb->s_time_gran = 1;
  777. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  778. if (!inode) {
  779. err = -ENOMEM;
  780. goto fail;
  781. }
  782. inode->i_op = &efivarfs_dir_inode_operations;
  783. root = d_make_root(inode);
  784. sb->s_root = root;
  785. if (!root) {
  786. err = -ENOMEM;
  787. goto fail;
  788. }
  789. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  790. struct inode *inode;
  791. struct dentry *dentry, *root = efivarfs_sb->s_root;
  792. char *name;
  793. unsigned long size = 0;
  794. int len, i;
  795. len = utf16_strlen(entry->var.VariableName);
  796. /* GUID plus trailing NULL */
  797. name = kmalloc(len + 38, GFP_ATOMIC);
  798. for (i = 0; i < len; i++)
  799. name[i] = entry->var.VariableName[i] & 0xFF;
  800. name[len] = '-';
  801. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  802. name[len+GUID_LEN] = '\0';
  803. inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
  804. S_IFREG | 0644, 0);
  805. dentry = d_alloc_name(root, name);
  806. efivars->ops->get_variable(entry->var.VariableName,
  807. &entry->var.VendorGuid,
  808. &entry->var.Attributes,
  809. &size,
  810. NULL);
  811. mutex_lock(&inode->i_mutex);
  812. inode->i_private = entry;
  813. i_size_write(inode, size+4);
  814. mutex_unlock(&inode->i_mutex);
  815. d_add(dentry, inode);
  816. }
  817. return 0;
  818. fail:
  819. iput(inode);
  820. return err;
  821. }
  822. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  823. int flags, const char *dev_name, void *data)
  824. {
  825. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  826. }
  827. static void efivarfs_kill_sb(struct super_block *sb)
  828. {
  829. kill_litter_super(sb);
  830. efivarfs_sb = NULL;
  831. }
  832. static struct file_system_type efivarfs_type = {
  833. .name = "efivarfs",
  834. .mount = efivarfs_mount,
  835. .kill_sb = efivarfs_kill_sb,
  836. };
  837. static const struct inode_operations efivarfs_dir_inode_operations = {
  838. .lookup = simple_lookup,
  839. .unlink = efivarfs_unlink,
  840. .create = efivarfs_create,
  841. };
  842. static struct pstore_info efi_pstore_info;
  843. #ifdef CONFIG_PSTORE
  844. static int efi_pstore_open(struct pstore_info *psi)
  845. {
  846. struct efivars *efivars = psi->data;
  847. spin_lock(&efivars->lock);
  848. efivars->walk_entry = list_first_entry(&efivars->list,
  849. struct efivar_entry, list);
  850. return 0;
  851. }
  852. static int efi_pstore_close(struct pstore_info *psi)
  853. {
  854. struct efivars *efivars = psi->data;
  855. spin_unlock(&efivars->lock);
  856. return 0;
  857. }
  858. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  859. struct timespec *timespec,
  860. char **buf, struct pstore_info *psi)
  861. {
  862. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  863. struct efivars *efivars = psi->data;
  864. char name[DUMP_NAME_LEN];
  865. int i;
  866. unsigned int part, size;
  867. unsigned long time;
  868. while (&efivars->walk_entry->list != &efivars->list) {
  869. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  870. vendor)) {
  871. for (i = 0; i < DUMP_NAME_LEN; i++) {
  872. name[i] = efivars->walk_entry->var.VariableName[i];
  873. }
  874. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  875. *id = part;
  876. timespec->tv_sec = time;
  877. timespec->tv_nsec = 0;
  878. get_var_data_locked(efivars, &efivars->walk_entry->var);
  879. size = efivars->walk_entry->var.DataSize;
  880. *buf = kmalloc(size, GFP_KERNEL);
  881. if (*buf == NULL)
  882. return -ENOMEM;
  883. memcpy(*buf, efivars->walk_entry->var.Data,
  884. size);
  885. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  886. struct efivar_entry, list);
  887. return size;
  888. }
  889. }
  890. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  891. struct efivar_entry, list);
  892. }
  893. return 0;
  894. }
  895. static int efi_pstore_write(enum pstore_type_id type,
  896. enum kmsg_dump_reason reason, u64 *id,
  897. unsigned int part, size_t size, struct pstore_info *psi)
  898. {
  899. char name[DUMP_NAME_LEN];
  900. char stub_name[DUMP_NAME_LEN];
  901. efi_char16_t efi_name[DUMP_NAME_LEN];
  902. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  903. struct efivars *efivars = psi->data;
  904. struct efivar_entry *entry, *found = NULL;
  905. int i, ret = 0;
  906. sprintf(stub_name, "dump-type%u-%u-", type, part);
  907. sprintf(name, "%s%lu", stub_name, get_seconds());
  908. spin_lock(&efivars->lock);
  909. for (i = 0; i < DUMP_NAME_LEN; i++)
  910. efi_name[i] = stub_name[i];
  911. /*
  912. * Clean up any entries with the same name
  913. */
  914. list_for_each_entry(entry, &efivars->list, list) {
  915. get_var_data_locked(efivars, &entry->var);
  916. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  917. continue;
  918. if (utf16_strncmp(entry->var.VariableName, efi_name,
  919. utf16_strlen(efi_name)))
  920. continue;
  921. /* Needs to be a prefix */
  922. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  923. continue;
  924. /* found */
  925. found = entry;
  926. efivars->ops->set_variable(entry->var.VariableName,
  927. &entry->var.VendorGuid,
  928. PSTORE_EFI_ATTRIBUTES,
  929. 0, NULL);
  930. }
  931. if (found)
  932. list_del(&found->list);
  933. for (i = 0; i < DUMP_NAME_LEN; i++)
  934. efi_name[i] = name[i];
  935. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  936. size, psi->buf);
  937. spin_unlock(&efivars->lock);
  938. if (found)
  939. efivar_unregister(found);
  940. if (size)
  941. ret = efivar_create_sysfs_entry(efivars,
  942. utf16_strsize(efi_name,
  943. DUMP_NAME_LEN * 2),
  944. efi_name, &vendor);
  945. *id = part;
  946. return ret;
  947. };
  948. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  949. struct pstore_info *psi)
  950. {
  951. efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
  952. return 0;
  953. }
  954. #else
  955. static int efi_pstore_open(struct pstore_info *psi)
  956. {
  957. return 0;
  958. }
  959. static int efi_pstore_close(struct pstore_info *psi)
  960. {
  961. return 0;
  962. }
  963. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  964. struct timespec *timespec,
  965. char **buf, struct pstore_info *psi)
  966. {
  967. return -1;
  968. }
  969. static int efi_pstore_write(enum pstore_type_id type,
  970. enum kmsg_dump_reason reason, u64 *id,
  971. unsigned int part, size_t size, struct pstore_info *psi)
  972. {
  973. return 0;
  974. }
  975. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  976. struct pstore_info *psi)
  977. {
  978. return 0;
  979. }
  980. #endif
  981. static struct pstore_info efi_pstore_info = {
  982. .owner = THIS_MODULE,
  983. .name = "efi",
  984. .open = efi_pstore_open,
  985. .close = efi_pstore_close,
  986. .read = efi_pstore_read,
  987. .write = efi_pstore_write,
  988. .erase = efi_pstore_erase,
  989. };
  990. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  991. struct bin_attribute *bin_attr,
  992. char *buf, loff_t pos, size_t count)
  993. {
  994. struct efi_variable *new_var = (struct efi_variable *)buf;
  995. struct efivars *efivars = bin_attr->private;
  996. struct efivar_entry *search_efivar, *n;
  997. unsigned long strsize1, strsize2;
  998. efi_status_t status = EFI_NOT_FOUND;
  999. int found = 0;
  1000. if (!capable(CAP_SYS_ADMIN))
  1001. return -EACCES;
  1002. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  1003. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  1004. printk(KERN_ERR "efivars: Malformed variable content\n");
  1005. return -EINVAL;
  1006. }
  1007. spin_lock(&efivars->lock);
  1008. /*
  1009. * Does this variable already exist?
  1010. */
  1011. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1012. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1013. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  1014. if (strsize1 == strsize2 &&
  1015. !memcmp(&(search_efivar->var.VariableName),
  1016. new_var->VariableName, strsize1) &&
  1017. !efi_guidcmp(search_efivar->var.VendorGuid,
  1018. new_var->VendorGuid)) {
  1019. found = 1;
  1020. break;
  1021. }
  1022. }
  1023. if (found) {
  1024. spin_unlock(&efivars->lock);
  1025. return -EINVAL;
  1026. }
  1027. /* now *really* create the variable via EFI */
  1028. status = efivars->ops->set_variable(new_var->VariableName,
  1029. &new_var->VendorGuid,
  1030. new_var->Attributes,
  1031. new_var->DataSize,
  1032. new_var->Data);
  1033. if (status != EFI_SUCCESS) {
  1034. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1035. status);
  1036. spin_unlock(&efivars->lock);
  1037. return -EIO;
  1038. }
  1039. spin_unlock(&efivars->lock);
  1040. /* Create the entry in sysfs. Locking is not required here */
  1041. status = efivar_create_sysfs_entry(efivars,
  1042. utf16_strsize(new_var->VariableName,
  1043. 1024),
  1044. new_var->VariableName,
  1045. &new_var->VendorGuid);
  1046. if (status) {
  1047. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  1048. }
  1049. return count;
  1050. }
  1051. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  1052. struct bin_attribute *bin_attr,
  1053. char *buf, loff_t pos, size_t count)
  1054. {
  1055. struct efi_variable *del_var = (struct efi_variable *)buf;
  1056. struct efivars *efivars = bin_attr->private;
  1057. struct efivar_entry *search_efivar, *n;
  1058. unsigned long strsize1, strsize2;
  1059. efi_status_t status = EFI_NOT_FOUND;
  1060. int found = 0;
  1061. if (!capable(CAP_SYS_ADMIN))
  1062. return -EACCES;
  1063. spin_lock(&efivars->lock);
  1064. /*
  1065. * Does this variable already exist?
  1066. */
  1067. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1068. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1069. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  1070. if (strsize1 == strsize2 &&
  1071. !memcmp(&(search_efivar->var.VariableName),
  1072. del_var->VariableName, strsize1) &&
  1073. !efi_guidcmp(search_efivar->var.VendorGuid,
  1074. del_var->VendorGuid)) {
  1075. found = 1;
  1076. break;
  1077. }
  1078. }
  1079. if (!found) {
  1080. spin_unlock(&efivars->lock);
  1081. return -EINVAL;
  1082. }
  1083. /* force the Attributes/DataSize to 0 to ensure deletion */
  1084. del_var->Attributes = 0;
  1085. del_var->DataSize = 0;
  1086. status = efivars->ops->set_variable(del_var->VariableName,
  1087. &del_var->VendorGuid,
  1088. del_var->Attributes,
  1089. del_var->DataSize,
  1090. del_var->Data);
  1091. if (status != EFI_SUCCESS) {
  1092. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1093. status);
  1094. spin_unlock(&efivars->lock);
  1095. return -EIO;
  1096. }
  1097. list_del(&search_efivar->list);
  1098. /* We need to release this lock before unregistering. */
  1099. spin_unlock(&efivars->lock);
  1100. efivar_unregister(search_efivar);
  1101. /* It's dead Jim.... */
  1102. return count;
  1103. }
  1104. /*
  1105. * Let's not leave out systab information that snuck into
  1106. * the efivars driver
  1107. */
  1108. static ssize_t systab_show(struct kobject *kobj,
  1109. struct kobj_attribute *attr, char *buf)
  1110. {
  1111. char *str = buf;
  1112. if (!kobj || !buf)
  1113. return -EINVAL;
  1114. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1115. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1116. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1117. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1118. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1119. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1120. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1121. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1122. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1123. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1124. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1125. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1126. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1127. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1128. return str - buf;
  1129. }
  1130. static struct kobj_attribute efi_attr_systab =
  1131. __ATTR(systab, 0400, systab_show, NULL);
  1132. static struct attribute *efi_subsys_attrs[] = {
  1133. &efi_attr_systab.attr,
  1134. NULL, /* maybe more in the future? */
  1135. };
  1136. static struct attribute_group efi_subsys_attr_group = {
  1137. .attrs = efi_subsys_attrs,
  1138. };
  1139. static struct kobject *efi_kobj;
  1140. /*
  1141. * efivar_create_sysfs_entry()
  1142. * Requires:
  1143. * variable_name_size = number of bytes required to hold
  1144. * variable_name (not counting the NULL
  1145. * character at the end.
  1146. * efivars->lock is not held on entry or exit.
  1147. * Returns 1 on failure, 0 on success
  1148. */
  1149. static int
  1150. efivar_create_sysfs_entry(struct efivars *efivars,
  1151. unsigned long variable_name_size,
  1152. efi_char16_t *variable_name,
  1153. efi_guid_t *vendor_guid)
  1154. {
  1155. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  1156. char *short_name;
  1157. struct efivar_entry *new_efivar;
  1158. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  1159. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  1160. if (!short_name || !new_efivar) {
  1161. kfree(short_name);
  1162. kfree(new_efivar);
  1163. return 1;
  1164. }
  1165. new_efivar->efivars = efivars;
  1166. memcpy(new_efivar->var.VariableName, variable_name,
  1167. variable_name_size);
  1168. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1169. /* Convert Unicode to normal chars (assume top bits are 0),
  1170. ala UTF-8 */
  1171. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1172. short_name[i] = variable_name[i] & 0xFF;
  1173. }
  1174. /* This is ugly, but necessary to separate one vendor's
  1175. private variables from another's. */
  1176. *(short_name + strlen(short_name)) = '-';
  1177. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1178. new_efivar->kobj.kset = efivars->kset;
  1179. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1180. "%s", short_name);
  1181. if (i) {
  1182. kfree(short_name);
  1183. kfree(new_efivar);
  1184. return 1;
  1185. }
  1186. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1187. kfree(short_name);
  1188. short_name = NULL;
  1189. spin_lock(&efivars->lock);
  1190. list_add(&new_efivar->list, &efivars->list);
  1191. spin_unlock(&efivars->lock);
  1192. return 0;
  1193. }
  1194. static int
  1195. create_efivars_bin_attributes(struct efivars *efivars)
  1196. {
  1197. struct bin_attribute *attr;
  1198. int error;
  1199. /* new_var */
  1200. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1201. if (!attr)
  1202. return -ENOMEM;
  1203. attr->attr.name = "new_var";
  1204. attr->attr.mode = 0200;
  1205. attr->write = efivar_create;
  1206. attr->private = efivars;
  1207. efivars->new_var = attr;
  1208. /* del_var */
  1209. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1210. if (!attr) {
  1211. error = -ENOMEM;
  1212. goto out_free;
  1213. }
  1214. attr->attr.name = "del_var";
  1215. attr->attr.mode = 0200;
  1216. attr->write = efivar_delete;
  1217. attr->private = efivars;
  1218. efivars->del_var = attr;
  1219. sysfs_bin_attr_init(efivars->new_var);
  1220. sysfs_bin_attr_init(efivars->del_var);
  1221. /* Register */
  1222. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1223. efivars->new_var);
  1224. if (error) {
  1225. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1226. " due to error %d\n", error);
  1227. goto out_free;
  1228. }
  1229. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1230. efivars->del_var);
  1231. if (error) {
  1232. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1233. " due to error %d\n", error);
  1234. sysfs_remove_bin_file(&efivars->kset->kobj,
  1235. efivars->new_var);
  1236. goto out_free;
  1237. }
  1238. return 0;
  1239. out_free:
  1240. kfree(efivars->del_var);
  1241. efivars->del_var = NULL;
  1242. kfree(efivars->new_var);
  1243. efivars->new_var = NULL;
  1244. return error;
  1245. }
  1246. void unregister_efivars(struct efivars *efivars)
  1247. {
  1248. struct efivar_entry *entry, *n;
  1249. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1250. spin_lock(&efivars->lock);
  1251. list_del(&entry->list);
  1252. spin_unlock(&efivars->lock);
  1253. efivar_unregister(entry);
  1254. }
  1255. if (efivars->new_var)
  1256. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1257. if (efivars->del_var)
  1258. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1259. kfree(efivars->new_var);
  1260. kfree(efivars->del_var);
  1261. kset_unregister(efivars->kset);
  1262. }
  1263. EXPORT_SYMBOL_GPL(unregister_efivars);
  1264. int register_efivars(struct efivars *efivars,
  1265. const struct efivar_operations *ops,
  1266. struct kobject *parent_kobj)
  1267. {
  1268. efi_status_t status = EFI_NOT_FOUND;
  1269. efi_guid_t vendor_guid;
  1270. efi_char16_t *variable_name;
  1271. unsigned long variable_name_size = 1024;
  1272. int error = 0;
  1273. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1274. if (!variable_name) {
  1275. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1276. return -ENOMEM;
  1277. }
  1278. spin_lock_init(&efivars->lock);
  1279. INIT_LIST_HEAD(&efivars->list);
  1280. efivars->ops = ops;
  1281. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1282. if (!efivars->kset) {
  1283. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1284. error = -ENOMEM;
  1285. goto out;
  1286. }
  1287. /*
  1288. * Per EFI spec, the maximum storage allocated for both
  1289. * the variable name and variable data is 1024 bytes.
  1290. */
  1291. do {
  1292. variable_name_size = 1024;
  1293. status = ops->get_next_variable(&variable_name_size,
  1294. variable_name,
  1295. &vendor_guid);
  1296. switch (status) {
  1297. case EFI_SUCCESS:
  1298. efivar_create_sysfs_entry(efivars,
  1299. variable_name_size,
  1300. variable_name,
  1301. &vendor_guid);
  1302. break;
  1303. case EFI_NOT_FOUND:
  1304. break;
  1305. default:
  1306. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1307. status);
  1308. status = EFI_NOT_FOUND;
  1309. break;
  1310. }
  1311. } while (status != EFI_NOT_FOUND);
  1312. error = create_efivars_bin_attributes(efivars);
  1313. if (error)
  1314. unregister_efivars(efivars);
  1315. efivars->efi_pstore_info = efi_pstore_info;
  1316. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1317. if (efivars->efi_pstore_info.buf) {
  1318. efivars->efi_pstore_info.bufsize = 1024;
  1319. efivars->efi_pstore_info.data = efivars;
  1320. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1321. pstore_register(&efivars->efi_pstore_info);
  1322. }
  1323. register_filesystem(&efivarfs_type);
  1324. out:
  1325. kfree(variable_name);
  1326. return error;
  1327. }
  1328. EXPORT_SYMBOL_GPL(register_efivars);
  1329. /*
  1330. * For now we register the efi subsystem with the firmware subsystem
  1331. * and the vars subsystem with the efi subsystem. In the future, it
  1332. * might make sense to split off the efi subsystem into its own
  1333. * driver, but for now only efivars will register with it, so just
  1334. * include it here.
  1335. */
  1336. static int __init
  1337. efivars_init(void)
  1338. {
  1339. int error = 0;
  1340. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1341. EFIVARS_DATE);
  1342. if (!efi_enabled)
  1343. return 0;
  1344. /* For now we'll register the efi directory at /sys/firmware/efi */
  1345. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1346. if (!efi_kobj) {
  1347. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1348. return -ENOMEM;
  1349. }
  1350. ops.get_variable = efi.get_variable;
  1351. ops.set_variable = efi.set_variable;
  1352. ops.get_next_variable = efi.get_next_variable;
  1353. error = register_efivars(&__efivars, &ops, efi_kobj);
  1354. if (error)
  1355. goto err_put;
  1356. /* Don't forget the systab entry */
  1357. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1358. if (error) {
  1359. printk(KERN_ERR
  1360. "efivars: Sysfs attribute export failed with error %d.\n",
  1361. error);
  1362. goto err_unregister;
  1363. }
  1364. return 0;
  1365. err_unregister:
  1366. unregister_efivars(&__efivars);
  1367. err_put:
  1368. kobject_put(efi_kobj);
  1369. return error;
  1370. }
  1371. static void __exit
  1372. efivars_exit(void)
  1373. {
  1374. if (efi_enabled) {
  1375. unregister_efivars(&__efivars);
  1376. kobject_put(efi_kobj);
  1377. }
  1378. }
  1379. module_init(efivars_init);
  1380. module_exit(efivars_exit);