efivars.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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 <asm/uaccess.h>
  82. #define EFIVARS_VERSION "0.08"
  83. #define EFIVARS_DATE "2004-May-17"
  84. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  85. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  86. MODULE_LICENSE("GPL");
  87. MODULE_VERSION(EFIVARS_VERSION);
  88. #define DUMP_NAME_LEN 52
  89. static bool efivars_pstore_disable =
  90. IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
  91. module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
  92. /*
  93. * The maximum size of VariableName + Data = 1024
  94. * Therefore, it's reasonable to save that much
  95. * space in each part of the structure,
  96. * and we use a page for reading/writing.
  97. */
  98. struct efi_variable {
  99. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  100. efi_guid_t VendorGuid;
  101. unsigned long DataSize;
  102. __u8 Data[1024];
  103. efi_status_t Status;
  104. __u32 Attributes;
  105. } __attribute__((packed));
  106. struct efivar_entry {
  107. struct efivars *efivars;
  108. struct efi_variable var;
  109. struct list_head list;
  110. struct kobject kobj;
  111. };
  112. struct efivar_attribute {
  113. struct attribute attr;
  114. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  115. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  116. };
  117. static struct efivars __efivars;
  118. static struct efivar_operations ops;
  119. #define PSTORE_EFI_ATTRIBUTES \
  120. (EFI_VARIABLE_NON_VOLATILE | \
  121. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  122. EFI_VARIABLE_RUNTIME_ACCESS)
  123. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  124. struct efivar_attribute efivar_attr_##_name = { \
  125. .attr = {.name = __stringify(_name), .mode = _mode}, \
  126. .show = _show, \
  127. .store = _store, \
  128. };
  129. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  130. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  131. /*
  132. * Prototype for sysfs creation function
  133. */
  134. static int
  135. efivar_create_sysfs_entry(struct efivars *efivars,
  136. unsigned long variable_name_size,
  137. efi_char16_t *variable_name,
  138. efi_guid_t *vendor_guid);
  139. /*
  140. * Prototype for workqueue functions updating sysfs entry
  141. */
  142. static void efivar_update_sysfs_entries(struct work_struct *);
  143. static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
  144. /* Return the number of unicode characters in data */
  145. static unsigned long
  146. utf16_strnlen(efi_char16_t *s, size_t maxlength)
  147. {
  148. unsigned long length = 0;
  149. while (*s++ != 0 && length < maxlength)
  150. length++;
  151. return length;
  152. }
  153. static inline unsigned long
  154. utf16_strlen(efi_char16_t *s)
  155. {
  156. return utf16_strnlen(s, ~0UL);
  157. }
  158. /*
  159. * Return the number of bytes is the length of this string
  160. * Note: this is NOT the same as the number of unicode characters
  161. */
  162. static inline unsigned long
  163. utf16_strsize(efi_char16_t *data, unsigned long maxlength)
  164. {
  165. return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
  166. }
  167. static inline int
  168. utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
  169. {
  170. while (1) {
  171. if (len == 0)
  172. return 0;
  173. if (*a < *b)
  174. return -1;
  175. if (*a > *b)
  176. return 1;
  177. if (*a == 0) /* implies *b == 0 */
  178. return 0;
  179. a++;
  180. b++;
  181. len--;
  182. }
  183. }
  184. static bool
  185. validate_device_path(struct efi_variable *var, int match, u8 *buffer,
  186. unsigned long len)
  187. {
  188. struct efi_generic_dev_path *node;
  189. int offset = 0;
  190. node = (struct efi_generic_dev_path *)buffer;
  191. if (len < sizeof(*node))
  192. return false;
  193. while (offset <= len - sizeof(*node) &&
  194. node->length >= sizeof(*node) &&
  195. node->length <= len - offset) {
  196. offset += node->length;
  197. if ((node->type == EFI_DEV_END_PATH ||
  198. node->type == EFI_DEV_END_PATH2) &&
  199. node->sub_type == EFI_DEV_END_ENTIRE)
  200. return true;
  201. node = (struct efi_generic_dev_path *)(buffer + offset);
  202. }
  203. /*
  204. * If we're here then either node->length pointed past the end
  205. * of the buffer or we reached the end of the buffer without
  206. * finding a device path end node.
  207. */
  208. return false;
  209. }
  210. static bool
  211. validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
  212. unsigned long len)
  213. {
  214. /* An array of 16-bit integers */
  215. if ((len % 2) != 0)
  216. return false;
  217. return true;
  218. }
  219. static bool
  220. validate_load_option(struct efi_variable *var, int match, u8 *buffer,
  221. unsigned long len)
  222. {
  223. u16 filepathlength;
  224. int i, desclength = 0, namelen;
  225. namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
  226. /* Either "Boot" or "Driver" followed by four digits of hex */
  227. for (i = match; i < match+4; i++) {
  228. if (var->VariableName[i] > 127 ||
  229. hex_to_bin(var->VariableName[i] & 0xff) < 0)
  230. return true;
  231. }
  232. /* Reject it if there's 4 digits of hex and then further content */
  233. if (namelen > match + 4)
  234. return false;
  235. /* A valid entry must be at least 8 bytes */
  236. if (len < 8)
  237. return false;
  238. filepathlength = buffer[4] | buffer[5] << 8;
  239. /*
  240. * There's no stored length for the description, so it has to be
  241. * found by hand
  242. */
  243. desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  244. /* Each boot entry must have a descriptor */
  245. if (!desclength)
  246. return false;
  247. /*
  248. * If the sum of the length of the description, the claimed filepath
  249. * length and the original header are greater than the length of the
  250. * variable, it's malformed
  251. */
  252. if ((desclength + filepathlength + 6) > len)
  253. return false;
  254. /*
  255. * And, finally, check the filepath
  256. */
  257. return validate_device_path(var, match, buffer + desclength + 6,
  258. filepathlength);
  259. }
  260. static bool
  261. validate_uint16(struct efi_variable *var, int match, u8 *buffer,
  262. unsigned long len)
  263. {
  264. /* A single 16-bit integer */
  265. if (len != 2)
  266. return false;
  267. return true;
  268. }
  269. static bool
  270. validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
  271. unsigned long len)
  272. {
  273. int i;
  274. for (i = 0; i < len; i++) {
  275. if (buffer[i] > 127)
  276. return false;
  277. if (buffer[i] == 0)
  278. return true;
  279. }
  280. return false;
  281. }
  282. struct variable_validate {
  283. char *name;
  284. bool (*validate)(struct efi_variable *var, int match, u8 *data,
  285. unsigned long len);
  286. };
  287. static const struct variable_validate variable_validate[] = {
  288. { "BootNext", validate_uint16 },
  289. { "BootOrder", validate_boot_order },
  290. { "DriverOrder", validate_boot_order },
  291. { "Boot*", validate_load_option },
  292. { "Driver*", validate_load_option },
  293. { "ConIn", validate_device_path },
  294. { "ConInDev", validate_device_path },
  295. { "ConOut", validate_device_path },
  296. { "ConOutDev", validate_device_path },
  297. { "ErrOut", validate_device_path },
  298. { "ErrOutDev", validate_device_path },
  299. { "Timeout", validate_uint16 },
  300. { "Lang", validate_ascii_string },
  301. { "PlatformLang", validate_ascii_string },
  302. { "", NULL },
  303. };
  304. static bool
  305. validate_var(struct efi_variable *var, u8 *data, unsigned long len)
  306. {
  307. int i;
  308. u16 *unicode_name = var->VariableName;
  309. for (i = 0; variable_validate[i].validate != NULL; i++) {
  310. const char *name = variable_validate[i].name;
  311. int match;
  312. for (match = 0; ; match++) {
  313. char c = name[match];
  314. u16 u = unicode_name[match];
  315. /* All special variables are plain ascii */
  316. if (u > 127)
  317. return true;
  318. /* Wildcard in the matching name means we've matched */
  319. if (c == '*')
  320. return variable_validate[i].validate(var,
  321. match, data, len);
  322. /* Case sensitive match */
  323. if (c != u)
  324. break;
  325. /* Reached the end of the string while matching */
  326. if (!c)
  327. return variable_validate[i].validate(var,
  328. match, data, len);
  329. }
  330. }
  331. return true;
  332. }
  333. static efi_status_t
  334. get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
  335. {
  336. efi_status_t status;
  337. var->DataSize = 1024;
  338. status = efivars->ops->get_variable(var->VariableName,
  339. &var->VendorGuid,
  340. &var->Attributes,
  341. &var->DataSize,
  342. var->Data);
  343. return status;
  344. }
  345. static efi_status_t
  346. get_var_data(struct efivars *efivars, struct efi_variable *var)
  347. {
  348. efi_status_t status;
  349. unsigned long flags;
  350. spin_lock_irqsave(&efivars->lock, flags);
  351. status = get_var_data_locked(efivars, var);
  352. spin_unlock_irqrestore(&efivars->lock, flags);
  353. if (status != EFI_SUCCESS) {
  354. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  355. status);
  356. }
  357. return status;
  358. }
  359. static efi_status_t
  360. check_var_size_locked(struct efivars *efivars, u32 attributes,
  361. unsigned long size)
  362. {
  363. const struct efivar_operations *fops = efivars->ops;
  364. if (!efivars->ops->query_variable_store)
  365. return EFI_UNSUPPORTED;
  366. return fops->query_variable_store(attributes, size);
  367. }
  368. static ssize_t
  369. efivar_guid_read(struct efivar_entry *entry, char *buf)
  370. {
  371. struct efi_variable *var = &entry->var;
  372. char *str = buf;
  373. if (!entry || !buf)
  374. return 0;
  375. efi_guid_unparse(&var->VendorGuid, str);
  376. str += strlen(str);
  377. str += sprintf(str, "\n");
  378. return str - buf;
  379. }
  380. static ssize_t
  381. efivar_attr_read(struct efivar_entry *entry, char *buf)
  382. {
  383. struct efi_variable *var = &entry->var;
  384. char *str = buf;
  385. efi_status_t status;
  386. if (!entry || !buf)
  387. return -EINVAL;
  388. status = get_var_data(entry->efivars, var);
  389. if (status != EFI_SUCCESS)
  390. return -EIO;
  391. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  392. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  393. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  394. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  395. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  396. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  397. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  398. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  399. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  400. str += sprintf(str,
  401. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  402. if (var->Attributes &
  403. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  404. str += sprintf(str,
  405. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  406. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  407. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  408. return str - buf;
  409. }
  410. static ssize_t
  411. efivar_size_read(struct efivar_entry *entry, char *buf)
  412. {
  413. struct efi_variable *var = &entry->var;
  414. char *str = buf;
  415. efi_status_t status;
  416. if (!entry || !buf)
  417. return -EINVAL;
  418. status = get_var_data(entry->efivars, var);
  419. if (status != EFI_SUCCESS)
  420. return -EIO;
  421. str += sprintf(str, "0x%lx\n", var->DataSize);
  422. return str - buf;
  423. }
  424. static ssize_t
  425. efivar_data_read(struct efivar_entry *entry, char *buf)
  426. {
  427. struct efi_variable *var = &entry->var;
  428. efi_status_t status;
  429. if (!entry || !buf)
  430. return -EINVAL;
  431. status = get_var_data(entry->efivars, var);
  432. if (status != EFI_SUCCESS)
  433. return -EIO;
  434. memcpy(buf, var->Data, var->DataSize);
  435. return var->DataSize;
  436. }
  437. /*
  438. * We allow each variable to be edited via rewriting the
  439. * entire efi variable structure.
  440. */
  441. static ssize_t
  442. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  443. {
  444. struct efi_variable *new_var, *var = &entry->var;
  445. struct efivars *efivars = entry->efivars;
  446. efi_status_t status = EFI_NOT_FOUND;
  447. if (count != sizeof(struct efi_variable))
  448. return -EINVAL;
  449. new_var = (struct efi_variable *)buf;
  450. /*
  451. * If only updating the variable data, then the name
  452. * and guid should remain the same
  453. */
  454. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  455. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  456. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  457. return -EINVAL;
  458. }
  459. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  460. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  461. return -EINVAL;
  462. }
  463. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  464. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  465. printk(KERN_ERR "efivars: Malformed variable content\n");
  466. return -EINVAL;
  467. }
  468. spin_lock_irq(&efivars->lock);
  469. status = check_var_size_locked(efivars, new_var->Attributes,
  470. new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
  471. if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
  472. status = efivars->ops->set_variable(new_var->VariableName,
  473. &new_var->VendorGuid,
  474. new_var->Attributes,
  475. new_var->DataSize,
  476. new_var->Data);
  477. spin_unlock_irq(&efivars->lock);
  478. if (status != EFI_SUCCESS) {
  479. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  480. status);
  481. return -EIO;
  482. }
  483. memcpy(&entry->var, new_var, count);
  484. return count;
  485. }
  486. static ssize_t
  487. efivar_show_raw(struct efivar_entry *entry, char *buf)
  488. {
  489. struct efi_variable *var = &entry->var;
  490. efi_status_t status;
  491. if (!entry || !buf)
  492. return 0;
  493. status = get_var_data(entry->efivars, var);
  494. if (status != EFI_SUCCESS)
  495. return -EIO;
  496. memcpy(buf, var, sizeof(*var));
  497. return sizeof(*var);
  498. }
  499. /*
  500. * Generic read/write functions that call the specific functions of
  501. * the attributes...
  502. */
  503. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  504. char *buf)
  505. {
  506. struct efivar_entry *var = to_efivar_entry(kobj);
  507. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  508. ssize_t ret = -EIO;
  509. if (!capable(CAP_SYS_ADMIN))
  510. return -EACCES;
  511. if (efivar_attr->show) {
  512. ret = efivar_attr->show(var, buf);
  513. }
  514. return ret;
  515. }
  516. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  517. const char *buf, size_t count)
  518. {
  519. struct efivar_entry *var = to_efivar_entry(kobj);
  520. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  521. ssize_t ret = -EIO;
  522. if (!capable(CAP_SYS_ADMIN))
  523. return -EACCES;
  524. if (efivar_attr->store)
  525. ret = efivar_attr->store(var, buf, count);
  526. return ret;
  527. }
  528. static const struct sysfs_ops efivar_attr_ops = {
  529. .show = efivar_attr_show,
  530. .store = efivar_attr_store,
  531. };
  532. static void efivar_release(struct kobject *kobj)
  533. {
  534. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  535. kfree(var);
  536. }
  537. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  538. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  539. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  540. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  541. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  542. static struct attribute *def_attrs[] = {
  543. &efivar_attr_guid.attr,
  544. &efivar_attr_size.attr,
  545. &efivar_attr_attributes.attr,
  546. &efivar_attr_data.attr,
  547. &efivar_attr_raw_var.attr,
  548. NULL,
  549. };
  550. static struct kobj_type efivar_ktype = {
  551. .release = efivar_release,
  552. .sysfs_ops = &efivar_attr_ops,
  553. .default_attrs = def_attrs,
  554. };
  555. static inline void
  556. efivar_unregister(struct efivar_entry *var)
  557. {
  558. kobject_put(&var->kobj);
  559. }
  560. static int efi_status_to_err(efi_status_t status)
  561. {
  562. int err;
  563. switch (status) {
  564. case EFI_INVALID_PARAMETER:
  565. err = -EINVAL;
  566. break;
  567. case EFI_OUT_OF_RESOURCES:
  568. err = -ENOSPC;
  569. break;
  570. case EFI_DEVICE_ERROR:
  571. err = -EIO;
  572. break;
  573. case EFI_WRITE_PROTECTED:
  574. err = -EROFS;
  575. break;
  576. case EFI_SECURITY_VIOLATION:
  577. err = -EACCES;
  578. break;
  579. case EFI_NOT_FOUND:
  580. err = -ENOENT;
  581. break;
  582. default:
  583. err = -EINVAL;
  584. }
  585. return err;
  586. }
  587. #ifdef CONFIG_EFI_VARS_PSTORE
  588. static int efi_pstore_open(struct pstore_info *psi)
  589. {
  590. struct efivars *efivars = psi->data;
  591. spin_lock_irq(&efivars->lock);
  592. efivars->walk_entry = list_first_entry(&efivars->list,
  593. struct efivar_entry, list);
  594. return 0;
  595. }
  596. static int efi_pstore_close(struct pstore_info *psi)
  597. {
  598. struct efivars *efivars = psi->data;
  599. spin_unlock_irq(&efivars->lock);
  600. return 0;
  601. }
  602. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  603. struct timespec *timespec,
  604. char **buf, struct pstore_info *psi)
  605. {
  606. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  607. struct efivars *efivars = psi->data;
  608. char name[DUMP_NAME_LEN];
  609. int i;
  610. unsigned int part, size;
  611. unsigned long time;
  612. while (&efivars->walk_entry->list != &efivars->list) {
  613. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  614. vendor)) {
  615. for (i = 0; i < DUMP_NAME_LEN; i++) {
  616. name[i] = efivars->walk_entry->var.VariableName[i];
  617. }
  618. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  619. *id = part;
  620. timespec->tv_sec = time;
  621. timespec->tv_nsec = 0;
  622. get_var_data_locked(efivars, &efivars->walk_entry->var);
  623. size = efivars->walk_entry->var.DataSize;
  624. *buf = kmalloc(size, GFP_KERNEL);
  625. if (*buf == NULL)
  626. return -ENOMEM;
  627. memcpy(*buf, efivars->walk_entry->var.Data,
  628. size);
  629. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  630. struct efivar_entry, list);
  631. return size;
  632. }
  633. }
  634. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  635. struct efivar_entry, list);
  636. }
  637. return 0;
  638. }
  639. static int efi_pstore_write(enum pstore_type_id type,
  640. enum kmsg_dump_reason reason, u64 *id,
  641. unsigned int part, size_t size, struct pstore_info *psi)
  642. {
  643. char name[DUMP_NAME_LEN];
  644. char stub_name[DUMP_NAME_LEN];
  645. efi_char16_t efi_name[DUMP_NAME_LEN];
  646. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  647. struct efivars *efivars = psi->data;
  648. struct efivar_entry *entry, *found = NULL;
  649. int i, ret = 0;
  650. efi_status_t status = EFI_NOT_FOUND;
  651. unsigned long flags;
  652. sprintf(stub_name, "dump-type%u-%u-", type, part);
  653. sprintf(name, "%s%lu", stub_name, get_seconds());
  654. spin_lock_irqsave(&efivars->lock, flags);
  655. if (size) {
  656. /*
  657. * Check if there is a space enough to log.
  658. * size: a size of logging data
  659. * DUMP_NAME_LEN * 2: a maximum size of variable name
  660. */
  661. status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
  662. size + DUMP_NAME_LEN * 2);
  663. if (status) {
  664. spin_unlock_irqrestore(&efivars->lock, flags);
  665. *id = part;
  666. return -ENOSPC;
  667. }
  668. }
  669. for (i = 0; i < DUMP_NAME_LEN; i++)
  670. efi_name[i] = stub_name[i];
  671. /*
  672. * Clean up any entries with the same name
  673. */
  674. list_for_each_entry(entry, &efivars->list, list) {
  675. get_var_data_locked(efivars, &entry->var);
  676. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  677. continue;
  678. if (utf16_strncmp(entry->var.VariableName, efi_name,
  679. utf16_strlen(efi_name)))
  680. continue;
  681. /* Needs to be a prefix */
  682. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  683. continue;
  684. /* found */
  685. found = entry;
  686. efivars->ops->set_variable(entry->var.VariableName,
  687. &entry->var.VendorGuid,
  688. PSTORE_EFI_ATTRIBUTES,
  689. 0, NULL);
  690. }
  691. if (found)
  692. list_del(&found->list);
  693. for (i = 0; i < DUMP_NAME_LEN; i++)
  694. efi_name[i] = name[i];
  695. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  696. size, psi->buf);
  697. spin_unlock_irqrestore(&efivars->lock, flags);
  698. if (found)
  699. efivar_unregister(found);
  700. if (reason == KMSG_DUMP_OOPS)
  701. schedule_work(&efivar_work);
  702. *id = part;
  703. return ret;
  704. };
  705. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  706. struct pstore_info *psi)
  707. {
  708. efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
  709. return 0;
  710. }
  711. static struct pstore_info efi_pstore_info = {
  712. .owner = THIS_MODULE,
  713. .name = "efi",
  714. .open = efi_pstore_open,
  715. .close = efi_pstore_close,
  716. .read = efi_pstore_read,
  717. .write = efi_pstore_write,
  718. .erase = efi_pstore_erase,
  719. };
  720. static void efivar_pstore_register(struct efivars *efivars)
  721. {
  722. efivars->efi_pstore_info = efi_pstore_info;
  723. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  724. if (efivars->efi_pstore_info.buf) {
  725. efivars->efi_pstore_info.bufsize = 1024;
  726. efivars->efi_pstore_info.data = efivars;
  727. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  728. pstore_register(&efivars->efi_pstore_info);
  729. }
  730. }
  731. #else
  732. static void efivar_pstore_register(struct efivars *efivars)
  733. {
  734. return;
  735. }
  736. #endif
  737. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  738. struct bin_attribute *bin_attr,
  739. char *buf, loff_t pos, size_t count)
  740. {
  741. struct efi_variable *new_var = (struct efi_variable *)buf;
  742. struct efivars *efivars = bin_attr->private;
  743. struct efivar_entry *search_efivar, *n;
  744. unsigned long strsize1, strsize2;
  745. efi_status_t status = EFI_NOT_FOUND;
  746. int found = 0;
  747. if (!capable(CAP_SYS_ADMIN))
  748. return -EACCES;
  749. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  750. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  751. printk(KERN_ERR "efivars: Malformed variable content\n");
  752. return -EINVAL;
  753. }
  754. spin_lock_irq(&efivars->lock);
  755. /*
  756. * Does this variable already exist?
  757. */
  758. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  759. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  760. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  761. if (strsize1 == strsize2 &&
  762. !memcmp(&(search_efivar->var.VariableName),
  763. new_var->VariableName, strsize1) &&
  764. !efi_guidcmp(search_efivar->var.VendorGuid,
  765. new_var->VendorGuid)) {
  766. found = 1;
  767. break;
  768. }
  769. }
  770. if (found) {
  771. spin_unlock_irq(&efivars->lock);
  772. return -EINVAL;
  773. }
  774. status = check_var_size_locked(efivars, new_var->Attributes,
  775. new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
  776. if (status && status != EFI_UNSUPPORTED) {
  777. spin_unlock_irq(&efivars->lock);
  778. return efi_status_to_err(status);
  779. }
  780. /* now *really* create the variable via EFI */
  781. status = efivars->ops->set_variable(new_var->VariableName,
  782. &new_var->VendorGuid,
  783. new_var->Attributes,
  784. new_var->DataSize,
  785. new_var->Data);
  786. if (status != EFI_SUCCESS) {
  787. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  788. status);
  789. spin_unlock_irq(&efivars->lock);
  790. return -EIO;
  791. }
  792. spin_unlock_irq(&efivars->lock);
  793. /* Create the entry in sysfs. Locking is not required here */
  794. status = efivar_create_sysfs_entry(efivars,
  795. utf16_strsize(new_var->VariableName,
  796. 1024),
  797. new_var->VariableName,
  798. &new_var->VendorGuid);
  799. if (status) {
  800. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  801. }
  802. return count;
  803. }
  804. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  805. struct bin_attribute *bin_attr,
  806. char *buf, loff_t pos, size_t count)
  807. {
  808. struct efi_variable *del_var = (struct efi_variable *)buf;
  809. struct efivars *efivars = bin_attr->private;
  810. struct efivar_entry *search_efivar, *n;
  811. unsigned long strsize1, strsize2;
  812. efi_status_t status = EFI_NOT_FOUND;
  813. int found = 0;
  814. if (!capable(CAP_SYS_ADMIN))
  815. return -EACCES;
  816. spin_lock_irq(&efivars->lock);
  817. /*
  818. * Does this variable already exist?
  819. */
  820. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  821. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  822. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  823. if (strsize1 == strsize2 &&
  824. !memcmp(&(search_efivar->var.VariableName),
  825. del_var->VariableName, strsize1) &&
  826. !efi_guidcmp(search_efivar->var.VendorGuid,
  827. del_var->VendorGuid)) {
  828. found = 1;
  829. break;
  830. }
  831. }
  832. if (!found) {
  833. spin_unlock_irq(&efivars->lock);
  834. return -EINVAL;
  835. }
  836. /* force the Attributes/DataSize to 0 to ensure deletion */
  837. del_var->Attributes = 0;
  838. del_var->DataSize = 0;
  839. status = efivars->ops->set_variable(del_var->VariableName,
  840. &del_var->VendorGuid,
  841. del_var->Attributes,
  842. del_var->DataSize,
  843. del_var->Data);
  844. if (status != EFI_SUCCESS) {
  845. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  846. status);
  847. spin_unlock_irq(&efivars->lock);
  848. return -EIO;
  849. }
  850. list_del(&search_efivar->list);
  851. /* We need to release this lock before unregistering. */
  852. spin_unlock_irq(&efivars->lock);
  853. efivar_unregister(search_efivar);
  854. /* It's dead Jim.... */
  855. return count;
  856. }
  857. static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
  858. {
  859. struct efivar_entry *entry, *n;
  860. struct efivars *efivars = &__efivars;
  861. unsigned long strsize1, strsize2;
  862. bool found = false;
  863. strsize1 = utf16_strsize(variable_name, 1024);
  864. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  865. strsize2 = utf16_strsize(entry->var.VariableName, 1024);
  866. if (strsize1 == strsize2 &&
  867. !memcmp(variable_name, &(entry->var.VariableName),
  868. strsize2) &&
  869. !efi_guidcmp(entry->var.VendorGuid,
  870. *vendor)) {
  871. found = true;
  872. break;
  873. }
  874. }
  875. return found;
  876. }
  877. static void efivar_update_sysfs_entries(struct work_struct *work)
  878. {
  879. struct efivars *efivars = &__efivars;
  880. efi_guid_t vendor;
  881. efi_char16_t *variable_name;
  882. unsigned long variable_name_size = 1024;
  883. efi_status_t status = EFI_NOT_FOUND;
  884. bool found;
  885. /* Add new sysfs entries */
  886. while (1) {
  887. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  888. if (!variable_name) {
  889. pr_err("efivars: Memory allocation failed.\n");
  890. return;
  891. }
  892. spin_lock_irq(&efivars->lock);
  893. found = false;
  894. while (1) {
  895. variable_name_size = 1024;
  896. status = efivars->ops->get_next_variable(
  897. &variable_name_size,
  898. variable_name,
  899. &vendor);
  900. if (status != EFI_SUCCESS) {
  901. break;
  902. } else {
  903. if (!variable_is_present(variable_name,
  904. &vendor)) {
  905. found = true;
  906. break;
  907. }
  908. }
  909. }
  910. spin_unlock_irq(&efivars->lock);
  911. if (!found) {
  912. kfree(variable_name);
  913. break;
  914. } else
  915. efivar_create_sysfs_entry(efivars,
  916. variable_name_size,
  917. variable_name, &vendor);
  918. }
  919. }
  920. /*
  921. * Returns the size of variable_name, in bytes, including the
  922. * terminating NULL character, or variable_name_size if no NULL
  923. * character is found among the first variable_name_size bytes.
  924. */
  925. static unsigned long var_name_strnsize(efi_char16_t *variable_name,
  926. unsigned long variable_name_size)
  927. {
  928. unsigned long len;
  929. efi_char16_t c;
  930. /*
  931. * The variable name is, by definition, a NULL-terminated
  932. * string, so make absolutely sure that variable_name_size is
  933. * the value we expect it to be. If not, return the real size.
  934. */
  935. for (len = 2; len <= variable_name_size; len += sizeof(c)) {
  936. c = variable_name[(len / sizeof(c)) - 1];
  937. if (!c)
  938. break;
  939. }
  940. return min(len, variable_name_size);
  941. }
  942. /*
  943. * Let's not leave out systab information that snuck into
  944. * the efivars driver
  945. */
  946. static ssize_t systab_show(struct kobject *kobj,
  947. struct kobj_attribute *attr, char *buf)
  948. {
  949. char *str = buf;
  950. if (!kobj || !buf)
  951. return -EINVAL;
  952. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  953. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  954. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  955. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  956. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  957. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  958. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  959. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  960. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  961. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  962. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  963. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  964. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  965. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  966. return str - buf;
  967. }
  968. static struct kobj_attribute efi_attr_systab =
  969. __ATTR(systab, 0400, systab_show, NULL);
  970. static struct attribute *efi_subsys_attrs[] = {
  971. &efi_attr_systab.attr,
  972. NULL, /* maybe more in the future? */
  973. };
  974. static struct attribute_group efi_subsys_attr_group = {
  975. .attrs = efi_subsys_attrs,
  976. };
  977. static struct kobject *efi_kobj;
  978. /*
  979. * efivar_create_sysfs_entry()
  980. * Requires:
  981. * variable_name_size = number of bytes required to hold
  982. * variable_name (not counting the NULL
  983. * character at the end.
  984. * efivars->lock is not held on entry or exit.
  985. * Returns 1 on failure, 0 on success
  986. */
  987. static int
  988. efivar_create_sysfs_entry(struct efivars *efivars,
  989. unsigned long variable_name_size,
  990. efi_char16_t *variable_name,
  991. efi_guid_t *vendor_guid)
  992. {
  993. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  994. char *short_name;
  995. struct efivar_entry *new_efivar;
  996. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  997. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  998. if (!short_name || !new_efivar) {
  999. kfree(short_name);
  1000. kfree(new_efivar);
  1001. return 1;
  1002. }
  1003. new_efivar->efivars = efivars;
  1004. memcpy(new_efivar->var.VariableName, variable_name,
  1005. variable_name_size);
  1006. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1007. /* Convert Unicode to normal chars (assume top bits are 0),
  1008. ala UTF-8 */
  1009. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1010. short_name[i] = variable_name[i] & 0xFF;
  1011. }
  1012. /* This is ugly, but necessary to separate one vendor's
  1013. private variables from another's. */
  1014. *(short_name + strlen(short_name)) = '-';
  1015. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1016. new_efivar->kobj.kset = efivars->kset;
  1017. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1018. "%s", short_name);
  1019. if (i) {
  1020. kfree(short_name);
  1021. kfree(new_efivar);
  1022. return 1;
  1023. }
  1024. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1025. kfree(short_name);
  1026. short_name = NULL;
  1027. spin_lock_irq(&efivars->lock);
  1028. list_add(&new_efivar->list, &efivars->list);
  1029. spin_unlock_irq(&efivars->lock);
  1030. return 0;
  1031. }
  1032. static int
  1033. create_efivars_bin_attributes(struct efivars *efivars)
  1034. {
  1035. struct bin_attribute *attr;
  1036. int error;
  1037. /* new_var */
  1038. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1039. if (!attr)
  1040. return -ENOMEM;
  1041. attr->attr.name = "new_var";
  1042. attr->attr.mode = 0200;
  1043. attr->write = efivar_create;
  1044. attr->private = efivars;
  1045. efivars->new_var = attr;
  1046. /* del_var */
  1047. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1048. if (!attr) {
  1049. error = -ENOMEM;
  1050. goto out_free;
  1051. }
  1052. attr->attr.name = "del_var";
  1053. attr->attr.mode = 0200;
  1054. attr->write = efivar_delete;
  1055. attr->private = efivars;
  1056. efivars->del_var = attr;
  1057. sysfs_bin_attr_init(efivars->new_var);
  1058. sysfs_bin_attr_init(efivars->del_var);
  1059. /* Register */
  1060. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1061. efivars->new_var);
  1062. if (error) {
  1063. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1064. " due to error %d\n", error);
  1065. goto out_free;
  1066. }
  1067. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1068. efivars->del_var);
  1069. if (error) {
  1070. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1071. " due to error %d\n", error);
  1072. sysfs_remove_bin_file(&efivars->kset->kobj,
  1073. efivars->new_var);
  1074. goto out_free;
  1075. }
  1076. return 0;
  1077. out_free:
  1078. kfree(efivars->del_var);
  1079. efivars->del_var = NULL;
  1080. kfree(efivars->new_var);
  1081. efivars->new_var = NULL;
  1082. return error;
  1083. }
  1084. void unregister_efivars(struct efivars *efivars)
  1085. {
  1086. struct efivar_entry *entry, *n;
  1087. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1088. spin_lock_irq(&efivars->lock);
  1089. list_del(&entry->list);
  1090. spin_unlock_irq(&efivars->lock);
  1091. efivar_unregister(entry);
  1092. }
  1093. if (efivars->new_var)
  1094. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1095. if (efivars->del_var)
  1096. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1097. kfree(efivars->new_var);
  1098. kfree(efivars->del_var);
  1099. kset_unregister(efivars->kset);
  1100. }
  1101. EXPORT_SYMBOL_GPL(unregister_efivars);
  1102. /*
  1103. * Print a warning when duplicate EFI variables are encountered and
  1104. * disable the sysfs workqueue since the firmware is buggy.
  1105. */
  1106. static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
  1107. unsigned long len16)
  1108. {
  1109. size_t i, len8 = len16 / sizeof(efi_char16_t);
  1110. char *s8;
  1111. s8 = kzalloc(len8, GFP_KERNEL);
  1112. if (!s8)
  1113. return;
  1114. for (i = 0; i < len8; i++)
  1115. s8[i] = s16[i];
  1116. printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
  1117. s8, vendor_guid);
  1118. kfree(s8);
  1119. }
  1120. int register_efivars(struct efivars *efivars,
  1121. const struct efivar_operations *ops,
  1122. struct kobject *parent_kobj)
  1123. {
  1124. efi_status_t status = EFI_NOT_FOUND;
  1125. efi_guid_t vendor_guid;
  1126. efi_char16_t *variable_name;
  1127. unsigned long variable_name_size = 1024;
  1128. int error = 0;
  1129. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1130. if (!variable_name) {
  1131. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1132. return -ENOMEM;
  1133. }
  1134. spin_lock_init(&efivars->lock);
  1135. INIT_LIST_HEAD(&efivars->list);
  1136. efivars->ops = ops;
  1137. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1138. if (!efivars->kset) {
  1139. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1140. error = -ENOMEM;
  1141. goto out;
  1142. }
  1143. /*
  1144. * Per EFI spec, the maximum storage allocated for both
  1145. * the variable name and variable data is 1024 bytes.
  1146. */
  1147. do {
  1148. variable_name_size = 1024;
  1149. status = ops->get_next_variable(&variable_name_size,
  1150. variable_name,
  1151. &vendor_guid);
  1152. switch (status) {
  1153. case EFI_SUCCESS:
  1154. variable_name_size = var_name_strnsize(variable_name,
  1155. variable_name_size);
  1156. /*
  1157. * Some firmware implementations return the
  1158. * same variable name on multiple calls to
  1159. * get_next_variable(). Terminate the loop
  1160. * immediately as there is no guarantee that
  1161. * we'll ever see a different variable name,
  1162. * and may end up looping here forever.
  1163. */
  1164. if (variable_is_present(variable_name, &vendor_guid)) {
  1165. dup_variable_bug(variable_name, &vendor_guid,
  1166. variable_name_size);
  1167. status = EFI_NOT_FOUND;
  1168. break;
  1169. }
  1170. efivar_create_sysfs_entry(efivars,
  1171. variable_name_size,
  1172. variable_name,
  1173. &vendor_guid);
  1174. break;
  1175. case EFI_NOT_FOUND:
  1176. break;
  1177. default:
  1178. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1179. status);
  1180. status = EFI_NOT_FOUND;
  1181. break;
  1182. }
  1183. } while (status != EFI_NOT_FOUND);
  1184. error = create_efivars_bin_attributes(efivars);
  1185. if (error)
  1186. unregister_efivars(efivars);
  1187. if (!efivars_pstore_disable)
  1188. efivar_pstore_register(efivars);
  1189. out:
  1190. kfree(variable_name);
  1191. return error;
  1192. }
  1193. EXPORT_SYMBOL_GPL(register_efivars);
  1194. /*
  1195. * For now we register the efi subsystem with the firmware subsystem
  1196. * and the vars subsystem with the efi subsystem. In the future, it
  1197. * might make sense to split off the efi subsystem into its own
  1198. * driver, but for now only efivars will register with it, so just
  1199. * include it here.
  1200. */
  1201. static int __init
  1202. efivars_init(void)
  1203. {
  1204. int error = 0;
  1205. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1206. EFIVARS_DATE);
  1207. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  1208. return 0;
  1209. /* For now we'll register the efi directory at /sys/firmware/efi */
  1210. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1211. if (!efi_kobj) {
  1212. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1213. return -ENOMEM;
  1214. }
  1215. ops.get_variable = efi.get_variable;
  1216. ops.set_variable = efi.set_variable;
  1217. ops.get_next_variable = efi.get_next_variable;
  1218. ops.query_variable_store = efi_query_variable_store;
  1219. error = register_efivars(&__efivars, &ops, efi_kobj);
  1220. if (error)
  1221. goto err_put;
  1222. /* Don't forget the systab entry */
  1223. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1224. if (error) {
  1225. printk(KERN_ERR
  1226. "efivars: Sysfs attribute export failed with error %d.\n",
  1227. error);
  1228. goto err_unregister;
  1229. }
  1230. return 0;
  1231. err_unregister:
  1232. unregister_efivars(&__efivars);
  1233. err_put:
  1234. kobject_put(efi_kobj);
  1235. return error;
  1236. }
  1237. static void __exit
  1238. efivars_exit(void)
  1239. {
  1240. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  1241. unregister_efivars(&__efivars);
  1242. kobject_put(efi_kobj);
  1243. }
  1244. }
  1245. module_init(efivars_init);
  1246. module_exit(efivars_exit);