efivars.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. unsigned long newdatasize;
  559. if (count < sizeof(attributes))
  560. return -EINVAL;
  561. data = kmalloc(datasize, GFP_KERNEL);
  562. if (!data)
  563. return -ENOMEM;
  564. efivars = var->efivars;
  565. if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
  566. count = -EFAULT;
  567. goto out;
  568. }
  569. if (attributes & ~(EFI_VARIABLE_MASK)) {
  570. count = -EINVAL;
  571. goto out;
  572. }
  573. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  574. count = -EFAULT;
  575. goto out;
  576. }
  577. if (validate_var(&var->var, data, datasize) == false) {
  578. count = -EINVAL;
  579. goto out;
  580. }
  581. status = efivars->ops->set_variable(var->var.VariableName,
  582. &var->var.VendorGuid,
  583. attributes, datasize,
  584. data);
  585. switch (status) {
  586. case EFI_SUCCESS:
  587. break;
  588. case EFI_INVALID_PARAMETER:
  589. count = -EINVAL;
  590. goto out;
  591. case EFI_OUT_OF_RESOURCES:
  592. count = -ENOSPC;
  593. goto out;
  594. case EFI_DEVICE_ERROR:
  595. count = -EIO;
  596. goto out;
  597. case EFI_WRITE_PROTECTED:
  598. count = -EROFS;
  599. goto out;
  600. case EFI_SECURITY_VIOLATION:
  601. count = -EACCES;
  602. goto out;
  603. case EFI_NOT_FOUND:
  604. count = -ENOENT;
  605. goto out;
  606. default:
  607. count = -EINVAL;
  608. goto out;
  609. }
  610. /*
  611. * Writing to the variable may have caused a change in size (which
  612. * could either be an append or an overwrite), or the variable to be
  613. * deleted. Perform a GetVariable() so we can tell what actually
  614. * happened.
  615. */
  616. newdatasize = 0;
  617. status = efivars->ops->get_variable(var->var.VariableName,
  618. &var->var.VendorGuid,
  619. NULL, &newdatasize,
  620. NULL);
  621. if (status == EFI_BUFFER_TOO_SMALL) {
  622. mutex_lock(&inode->i_mutex);
  623. i_size_write(inode, newdatasize + sizeof(attributes));
  624. mutex_unlock(&inode->i_mutex);
  625. } else if (status == EFI_NOT_FOUND) {
  626. spin_lock(&efivars->lock);
  627. list_del(&var->list);
  628. spin_unlock(&efivars->lock);
  629. efivar_unregister(var);
  630. drop_nlink(inode);
  631. dput(file->f_dentry);
  632. } else {
  633. pr_warn("efivarfs: inconsistent EFI variable implementation? "
  634. "status = %lx\n", status);
  635. }
  636. out:
  637. kfree(data);
  638. return count;
  639. }
  640. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  641. size_t count, loff_t *ppos)
  642. {
  643. struct efivar_entry *var = file->private_data;
  644. struct efivars *efivars = var->efivars;
  645. efi_status_t status;
  646. unsigned long datasize = 0;
  647. u32 attributes;
  648. void *data;
  649. ssize_t size = 0;
  650. status = efivars->ops->get_variable(var->var.VariableName,
  651. &var->var.VendorGuid,
  652. &attributes, &datasize, NULL);
  653. if (status != EFI_BUFFER_TOO_SMALL)
  654. return 0;
  655. data = kmalloc(datasize + 4, GFP_KERNEL);
  656. if (!data)
  657. return 0;
  658. status = efivars->ops->get_variable(var->var.VariableName,
  659. &var->var.VendorGuid,
  660. &attributes, &datasize,
  661. (data + 4));
  662. if (status != EFI_SUCCESS)
  663. goto out_free;
  664. memcpy(data, &attributes, 4);
  665. size = simple_read_from_buffer(userbuf, count, ppos,
  666. data, datasize + 4);
  667. out_free:
  668. kfree(data);
  669. return size;
  670. }
  671. static void efivarfs_evict_inode(struct inode *inode)
  672. {
  673. clear_inode(inode);
  674. }
  675. static const struct super_operations efivarfs_ops = {
  676. .statfs = simple_statfs,
  677. .drop_inode = generic_delete_inode,
  678. .evict_inode = efivarfs_evict_inode,
  679. .show_options = generic_show_options,
  680. };
  681. static struct super_block *efivarfs_sb;
  682. static const struct inode_operations efivarfs_dir_inode_operations;
  683. static const struct file_operations efivarfs_file_operations = {
  684. .open = efivarfs_file_open,
  685. .read = efivarfs_file_read,
  686. .write = efivarfs_file_write,
  687. .llseek = no_llseek,
  688. };
  689. static struct inode *efivarfs_get_inode(struct super_block *sb,
  690. const struct inode *dir, int mode, dev_t dev)
  691. {
  692. struct inode *inode = new_inode(sb);
  693. if (inode) {
  694. inode->i_ino = get_next_ino();
  695. inode->i_uid = inode->i_gid = 0;
  696. inode->i_mode = mode;
  697. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  698. switch (mode & S_IFMT) {
  699. case S_IFREG:
  700. inode->i_fop = &efivarfs_file_operations;
  701. break;
  702. case S_IFDIR:
  703. inode->i_op = &efivarfs_dir_inode_operations;
  704. inode->i_fop = &simple_dir_operations;
  705. inc_nlink(inode);
  706. break;
  707. }
  708. }
  709. return inode;
  710. }
  711. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  712. {
  713. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  714. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  715. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  716. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  717. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  718. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  719. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  720. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  721. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  722. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  723. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  724. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  725. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  726. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  727. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  728. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  729. }
  730. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  731. umode_t mode, bool excl)
  732. {
  733. struct inode *inode;
  734. struct efivars *efivars = &__efivars;
  735. struct efivar_entry *var;
  736. int namelen, i = 0, err = 0;
  737. if (dentry->d_name.len < 38)
  738. return -EINVAL;
  739. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  740. if (!inode)
  741. return -ENOSPC;
  742. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  743. if (!var) {
  744. err = -ENOMEM;
  745. goto out;
  746. }
  747. namelen = dentry->d_name.len - GUID_LEN;
  748. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  749. &var->var.VendorGuid);
  750. for (i = 0; i < namelen; i++)
  751. var->var.VariableName[i] = dentry->d_name.name[i];
  752. var->var.VariableName[i] = '\0';
  753. inode->i_private = var;
  754. var->efivars = efivars;
  755. var->kobj.kset = efivars->kset;
  756. err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
  757. dentry->d_name.name);
  758. if (err)
  759. goto out;
  760. kobject_uevent(&var->kobj, KOBJ_ADD);
  761. spin_lock(&efivars->lock);
  762. list_add(&var->list, &efivars->list);
  763. spin_unlock(&efivars->lock);
  764. d_instantiate(dentry, inode);
  765. dget(dentry);
  766. out:
  767. if (err) {
  768. kfree(var);
  769. iput(inode);
  770. }
  771. return err;
  772. }
  773. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  774. {
  775. struct efivar_entry *var = dentry->d_inode->i_private;
  776. struct efivars *efivars = var->efivars;
  777. efi_status_t status;
  778. spin_lock(&efivars->lock);
  779. status = efivars->ops->set_variable(var->var.VariableName,
  780. &var->var.VendorGuid,
  781. 0, 0, NULL);
  782. if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
  783. list_del(&var->list);
  784. spin_unlock(&efivars->lock);
  785. efivar_unregister(var);
  786. drop_nlink(dir);
  787. dput(dentry);
  788. return 0;
  789. }
  790. spin_unlock(&efivars->lock);
  791. return -EINVAL;
  792. };
  793. int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  794. {
  795. struct inode *inode = NULL;
  796. struct dentry *root;
  797. struct efivar_entry *entry, *n;
  798. struct efivars *efivars = &__efivars;
  799. int err;
  800. efivarfs_sb = sb;
  801. sb->s_maxbytes = MAX_LFS_FILESIZE;
  802. sb->s_blocksize = PAGE_CACHE_SIZE;
  803. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  804. sb->s_magic = PSTOREFS_MAGIC;
  805. sb->s_op = &efivarfs_ops;
  806. sb->s_time_gran = 1;
  807. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  808. if (!inode) {
  809. err = -ENOMEM;
  810. goto fail;
  811. }
  812. inode->i_op = &efivarfs_dir_inode_operations;
  813. root = d_make_root(inode);
  814. sb->s_root = root;
  815. if (!root) {
  816. err = -ENOMEM;
  817. goto fail;
  818. }
  819. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  820. struct inode *inode;
  821. struct dentry *dentry, *root = efivarfs_sb->s_root;
  822. char *name;
  823. unsigned long size = 0;
  824. int len, i;
  825. len = utf16_strlen(entry->var.VariableName);
  826. /* GUID plus trailing NULL */
  827. name = kmalloc(len + 38, GFP_ATOMIC);
  828. for (i = 0; i < len; i++)
  829. name[i] = entry->var.VariableName[i] & 0xFF;
  830. name[len] = '-';
  831. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  832. name[len+GUID_LEN] = '\0';
  833. inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
  834. S_IFREG | 0644, 0);
  835. dentry = d_alloc_name(root, name);
  836. efivars->ops->get_variable(entry->var.VariableName,
  837. &entry->var.VendorGuid,
  838. &entry->var.Attributes,
  839. &size,
  840. NULL);
  841. mutex_lock(&inode->i_mutex);
  842. inode->i_private = entry;
  843. i_size_write(inode, size+4);
  844. mutex_unlock(&inode->i_mutex);
  845. d_add(dentry, inode);
  846. }
  847. return 0;
  848. fail:
  849. iput(inode);
  850. return err;
  851. }
  852. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  853. int flags, const char *dev_name, void *data)
  854. {
  855. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  856. }
  857. static void efivarfs_kill_sb(struct super_block *sb)
  858. {
  859. kill_litter_super(sb);
  860. efivarfs_sb = NULL;
  861. }
  862. static struct file_system_type efivarfs_type = {
  863. .name = "efivarfs",
  864. .mount = efivarfs_mount,
  865. .kill_sb = efivarfs_kill_sb,
  866. };
  867. static const struct inode_operations efivarfs_dir_inode_operations = {
  868. .lookup = simple_lookup,
  869. .unlink = efivarfs_unlink,
  870. .create = efivarfs_create,
  871. };
  872. static struct pstore_info efi_pstore_info;
  873. #ifdef CONFIG_PSTORE
  874. static int efi_pstore_open(struct pstore_info *psi)
  875. {
  876. struct efivars *efivars = psi->data;
  877. spin_lock(&efivars->lock);
  878. efivars->walk_entry = list_first_entry(&efivars->list,
  879. struct efivar_entry, list);
  880. return 0;
  881. }
  882. static int efi_pstore_close(struct pstore_info *psi)
  883. {
  884. struct efivars *efivars = psi->data;
  885. spin_unlock(&efivars->lock);
  886. return 0;
  887. }
  888. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  889. struct timespec *timespec,
  890. char **buf, struct pstore_info *psi)
  891. {
  892. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  893. struct efivars *efivars = psi->data;
  894. char name[DUMP_NAME_LEN];
  895. int i;
  896. unsigned int part, size;
  897. unsigned long time;
  898. while (&efivars->walk_entry->list != &efivars->list) {
  899. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  900. vendor)) {
  901. for (i = 0; i < DUMP_NAME_LEN; i++) {
  902. name[i] = efivars->walk_entry->var.VariableName[i];
  903. }
  904. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  905. *id = part;
  906. timespec->tv_sec = time;
  907. timespec->tv_nsec = 0;
  908. get_var_data_locked(efivars, &efivars->walk_entry->var);
  909. size = efivars->walk_entry->var.DataSize;
  910. *buf = kmalloc(size, GFP_KERNEL);
  911. if (*buf == NULL)
  912. return -ENOMEM;
  913. memcpy(*buf, efivars->walk_entry->var.Data,
  914. size);
  915. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  916. struct efivar_entry, list);
  917. return size;
  918. }
  919. }
  920. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  921. struct efivar_entry, list);
  922. }
  923. return 0;
  924. }
  925. static int efi_pstore_write(enum pstore_type_id type,
  926. enum kmsg_dump_reason reason, u64 *id,
  927. unsigned int part, size_t size, struct pstore_info *psi)
  928. {
  929. char name[DUMP_NAME_LEN];
  930. char stub_name[DUMP_NAME_LEN];
  931. efi_char16_t efi_name[DUMP_NAME_LEN];
  932. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  933. struct efivars *efivars = psi->data;
  934. struct efivar_entry *entry, *found = NULL;
  935. int i, ret = 0;
  936. sprintf(stub_name, "dump-type%u-%u-", type, part);
  937. sprintf(name, "%s%lu", stub_name, get_seconds());
  938. spin_lock(&efivars->lock);
  939. for (i = 0; i < DUMP_NAME_LEN; i++)
  940. efi_name[i] = stub_name[i];
  941. /*
  942. * Clean up any entries with the same name
  943. */
  944. list_for_each_entry(entry, &efivars->list, list) {
  945. get_var_data_locked(efivars, &entry->var);
  946. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  947. continue;
  948. if (utf16_strncmp(entry->var.VariableName, efi_name,
  949. utf16_strlen(efi_name)))
  950. continue;
  951. /* Needs to be a prefix */
  952. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  953. continue;
  954. /* found */
  955. found = entry;
  956. efivars->ops->set_variable(entry->var.VariableName,
  957. &entry->var.VendorGuid,
  958. PSTORE_EFI_ATTRIBUTES,
  959. 0, NULL);
  960. }
  961. if (found)
  962. list_del(&found->list);
  963. for (i = 0; i < DUMP_NAME_LEN; i++)
  964. efi_name[i] = name[i];
  965. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  966. size, psi->buf);
  967. spin_unlock(&efivars->lock);
  968. if (found)
  969. efivar_unregister(found);
  970. if (size)
  971. ret = efivar_create_sysfs_entry(efivars,
  972. utf16_strsize(efi_name,
  973. DUMP_NAME_LEN * 2),
  974. efi_name, &vendor);
  975. *id = part;
  976. return ret;
  977. };
  978. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  979. struct pstore_info *psi)
  980. {
  981. efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
  982. return 0;
  983. }
  984. #else
  985. static int efi_pstore_open(struct pstore_info *psi)
  986. {
  987. return 0;
  988. }
  989. static int efi_pstore_close(struct pstore_info *psi)
  990. {
  991. return 0;
  992. }
  993. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  994. struct timespec *timespec,
  995. char **buf, struct pstore_info *psi)
  996. {
  997. return -1;
  998. }
  999. static int efi_pstore_write(enum pstore_type_id type,
  1000. enum kmsg_dump_reason reason, u64 *id,
  1001. unsigned int part, size_t size, struct pstore_info *psi)
  1002. {
  1003. return 0;
  1004. }
  1005. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  1006. struct pstore_info *psi)
  1007. {
  1008. return 0;
  1009. }
  1010. #endif
  1011. static struct pstore_info efi_pstore_info = {
  1012. .owner = THIS_MODULE,
  1013. .name = "efi",
  1014. .open = efi_pstore_open,
  1015. .close = efi_pstore_close,
  1016. .read = efi_pstore_read,
  1017. .write = efi_pstore_write,
  1018. .erase = efi_pstore_erase,
  1019. };
  1020. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  1021. struct bin_attribute *bin_attr,
  1022. char *buf, loff_t pos, size_t count)
  1023. {
  1024. struct efi_variable *new_var = (struct efi_variable *)buf;
  1025. struct efivars *efivars = bin_attr->private;
  1026. struct efivar_entry *search_efivar, *n;
  1027. unsigned long strsize1, strsize2;
  1028. efi_status_t status = EFI_NOT_FOUND;
  1029. int found = 0;
  1030. if (!capable(CAP_SYS_ADMIN))
  1031. return -EACCES;
  1032. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  1033. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  1034. printk(KERN_ERR "efivars: Malformed variable content\n");
  1035. return -EINVAL;
  1036. }
  1037. spin_lock(&efivars->lock);
  1038. /*
  1039. * Does this variable already exist?
  1040. */
  1041. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1042. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1043. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  1044. if (strsize1 == strsize2 &&
  1045. !memcmp(&(search_efivar->var.VariableName),
  1046. new_var->VariableName, strsize1) &&
  1047. !efi_guidcmp(search_efivar->var.VendorGuid,
  1048. new_var->VendorGuid)) {
  1049. found = 1;
  1050. break;
  1051. }
  1052. }
  1053. if (found) {
  1054. spin_unlock(&efivars->lock);
  1055. return -EINVAL;
  1056. }
  1057. /* now *really* create the variable via EFI */
  1058. status = efivars->ops->set_variable(new_var->VariableName,
  1059. &new_var->VendorGuid,
  1060. new_var->Attributes,
  1061. new_var->DataSize,
  1062. new_var->Data);
  1063. if (status != EFI_SUCCESS) {
  1064. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1065. status);
  1066. spin_unlock(&efivars->lock);
  1067. return -EIO;
  1068. }
  1069. spin_unlock(&efivars->lock);
  1070. /* Create the entry in sysfs. Locking is not required here */
  1071. status = efivar_create_sysfs_entry(efivars,
  1072. utf16_strsize(new_var->VariableName,
  1073. 1024),
  1074. new_var->VariableName,
  1075. &new_var->VendorGuid);
  1076. if (status) {
  1077. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  1078. }
  1079. return count;
  1080. }
  1081. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  1082. struct bin_attribute *bin_attr,
  1083. char *buf, loff_t pos, size_t count)
  1084. {
  1085. struct efi_variable *del_var = (struct efi_variable *)buf;
  1086. struct efivars *efivars = bin_attr->private;
  1087. struct efivar_entry *search_efivar, *n;
  1088. unsigned long strsize1, strsize2;
  1089. efi_status_t status = EFI_NOT_FOUND;
  1090. int found = 0;
  1091. if (!capable(CAP_SYS_ADMIN))
  1092. return -EACCES;
  1093. spin_lock(&efivars->lock);
  1094. /*
  1095. * Does this variable already exist?
  1096. */
  1097. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1098. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1099. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  1100. if (strsize1 == strsize2 &&
  1101. !memcmp(&(search_efivar->var.VariableName),
  1102. del_var->VariableName, strsize1) &&
  1103. !efi_guidcmp(search_efivar->var.VendorGuid,
  1104. del_var->VendorGuid)) {
  1105. found = 1;
  1106. break;
  1107. }
  1108. }
  1109. if (!found) {
  1110. spin_unlock(&efivars->lock);
  1111. return -EINVAL;
  1112. }
  1113. /* force the Attributes/DataSize to 0 to ensure deletion */
  1114. del_var->Attributes = 0;
  1115. del_var->DataSize = 0;
  1116. status = efivars->ops->set_variable(del_var->VariableName,
  1117. &del_var->VendorGuid,
  1118. del_var->Attributes,
  1119. del_var->DataSize,
  1120. del_var->Data);
  1121. if (status != EFI_SUCCESS) {
  1122. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1123. status);
  1124. spin_unlock(&efivars->lock);
  1125. return -EIO;
  1126. }
  1127. list_del(&search_efivar->list);
  1128. /* We need to release this lock before unregistering. */
  1129. spin_unlock(&efivars->lock);
  1130. efivar_unregister(search_efivar);
  1131. /* It's dead Jim.... */
  1132. return count;
  1133. }
  1134. /*
  1135. * Let's not leave out systab information that snuck into
  1136. * the efivars driver
  1137. */
  1138. static ssize_t systab_show(struct kobject *kobj,
  1139. struct kobj_attribute *attr, char *buf)
  1140. {
  1141. char *str = buf;
  1142. if (!kobj || !buf)
  1143. return -EINVAL;
  1144. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1145. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1146. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1147. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1148. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1149. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1150. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1151. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1152. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1153. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1154. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1155. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1156. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1157. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1158. return str - buf;
  1159. }
  1160. static struct kobj_attribute efi_attr_systab =
  1161. __ATTR(systab, 0400, systab_show, NULL);
  1162. static struct attribute *efi_subsys_attrs[] = {
  1163. &efi_attr_systab.attr,
  1164. NULL, /* maybe more in the future? */
  1165. };
  1166. static struct attribute_group efi_subsys_attr_group = {
  1167. .attrs = efi_subsys_attrs,
  1168. };
  1169. static struct kobject *efi_kobj;
  1170. /*
  1171. * efivar_create_sysfs_entry()
  1172. * Requires:
  1173. * variable_name_size = number of bytes required to hold
  1174. * variable_name (not counting the NULL
  1175. * character at the end.
  1176. * efivars->lock is not held on entry or exit.
  1177. * Returns 1 on failure, 0 on success
  1178. */
  1179. static int
  1180. efivar_create_sysfs_entry(struct efivars *efivars,
  1181. unsigned long variable_name_size,
  1182. efi_char16_t *variable_name,
  1183. efi_guid_t *vendor_guid)
  1184. {
  1185. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  1186. char *short_name;
  1187. struct efivar_entry *new_efivar;
  1188. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  1189. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  1190. if (!short_name || !new_efivar) {
  1191. kfree(short_name);
  1192. kfree(new_efivar);
  1193. return 1;
  1194. }
  1195. new_efivar->efivars = efivars;
  1196. memcpy(new_efivar->var.VariableName, variable_name,
  1197. variable_name_size);
  1198. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1199. /* Convert Unicode to normal chars (assume top bits are 0),
  1200. ala UTF-8 */
  1201. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1202. short_name[i] = variable_name[i] & 0xFF;
  1203. }
  1204. /* This is ugly, but necessary to separate one vendor's
  1205. private variables from another's. */
  1206. *(short_name + strlen(short_name)) = '-';
  1207. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1208. new_efivar->kobj.kset = efivars->kset;
  1209. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1210. "%s", short_name);
  1211. if (i) {
  1212. kfree(short_name);
  1213. kfree(new_efivar);
  1214. return 1;
  1215. }
  1216. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1217. kfree(short_name);
  1218. short_name = NULL;
  1219. spin_lock(&efivars->lock);
  1220. list_add(&new_efivar->list, &efivars->list);
  1221. spin_unlock(&efivars->lock);
  1222. return 0;
  1223. }
  1224. static int
  1225. create_efivars_bin_attributes(struct efivars *efivars)
  1226. {
  1227. struct bin_attribute *attr;
  1228. int error;
  1229. /* new_var */
  1230. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1231. if (!attr)
  1232. return -ENOMEM;
  1233. attr->attr.name = "new_var";
  1234. attr->attr.mode = 0200;
  1235. attr->write = efivar_create;
  1236. attr->private = efivars;
  1237. efivars->new_var = attr;
  1238. /* del_var */
  1239. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1240. if (!attr) {
  1241. error = -ENOMEM;
  1242. goto out_free;
  1243. }
  1244. attr->attr.name = "del_var";
  1245. attr->attr.mode = 0200;
  1246. attr->write = efivar_delete;
  1247. attr->private = efivars;
  1248. efivars->del_var = attr;
  1249. sysfs_bin_attr_init(efivars->new_var);
  1250. sysfs_bin_attr_init(efivars->del_var);
  1251. /* Register */
  1252. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1253. efivars->new_var);
  1254. if (error) {
  1255. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1256. " due to error %d\n", error);
  1257. goto out_free;
  1258. }
  1259. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1260. efivars->del_var);
  1261. if (error) {
  1262. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1263. " due to error %d\n", error);
  1264. sysfs_remove_bin_file(&efivars->kset->kobj,
  1265. efivars->new_var);
  1266. goto out_free;
  1267. }
  1268. return 0;
  1269. out_free:
  1270. kfree(efivars->del_var);
  1271. efivars->del_var = NULL;
  1272. kfree(efivars->new_var);
  1273. efivars->new_var = NULL;
  1274. return error;
  1275. }
  1276. void unregister_efivars(struct efivars *efivars)
  1277. {
  1278. struct efivar_entry *entry, *n;
  1279. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1280. spin_lock(&efivars->lock);
  1281. list_del(&entry->list);
  1282. spin_unlock(&efivars->lock);
  1283. efivar_unregister(entry);
  1284. }
  1285. if (efivars->new_var)
  1286. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1287. if (efivars->del_var)
  1288. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1289. kfree(efivars->new_var);
  1290. kfree(efivars->del_var);
  1291. kobject_put(efivars->kobject);
  1292. kset_unregister(efivars->kset);
  1293. }
  1294. EXPORT_SYMBOL_GPL(unregister_efivars);
  1295. int register_efivars(struct efivars *efivars,
  1296. const struct efivar_operations *ops,
  1297. struct kobject *parent_kobj)
  1298. {
  1299. efi_status_t status = EFI_NOT_FOUND;
  1300. efi_guid_t vendor_guid;
  1301. efi_char16_t *variable_name;
  1302. unsigned long variable_name_size = 1024;
  1303. int error = 0;
  1304. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1305. if (!variable_name) {
  1306. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1307. return -ENOMEM;
  1308. }
  1309. spin_lock_init(&efivars->lock);
  1310. INIT_LIST_HEAD(&efivars->list);
  1311. efivars->ops = ops;
  1312. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1313. if (!efivars->kset) {
  1314. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1315. error = -ENOMEM;
  1316. goto out;
  1317. }
  1318. efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
  1319. if (!efivars->kobject) {
  1320. pr_err("efivars: Subsystem registration failed.\n");
  1321. error = -ENOMEM;
  1322. kset_unregister(efivars->kset);
  1323. goto out;
  1324. }
  1325. /*
  1326. * Per EFI spec, the maximum storage allocated for both
  1327. * the variable name and variable data is 1024 bytes.
  1328. */
  1329. do {
  1330. variable_name_size = 1024;
  1331. status = ops->get_next_variable(&variable_name_size,
  1332. variable_name,
  1333. &vendor_guid);
  1334. switch (status) {
  1335. case EFI_SUCCESS:
  1336. efivar_create_sysfs_entry(efivars,
  1337. variable_name_size,
  1338. variable_name,
  1339. &vendor_guid);
  1340. break;
  1341. case EFI_NOT_FOUND:
  1342. break;
  1343. default:
  1344. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1345. status);
  1346. status = EFI_NOT_FOUND;
  1347. break;
  1348. }
  1349. } while (status != EFI_NOT_FOUND);
  1350. error = create_efivars_bin_attributes(efivars);
  1351. if (error)
  1352. unregister_efivars(efivars);
  1353. efivars->efi_pstore_info = efi_pstore_info;
  1354. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1355. if (efivars->efi_pstore_info.buf) {
  1356. efivars->efi_pstore_info.bufsize = 1024;
  1357. efivars->efi_pstore_info.data = efivars;
  1358. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1359. pstore_register(&efivars->efi_pstore_info);
  1360. }
  1361. register_filesystem(&efivarfs_type);
  1362. out:
  1363. kfree(variable_name);
  1364. return error;
  1365. }
  1366. EXPORT_SYMBOL_GPL(register_efivars);
  1367. /*
  1368. * For now we register the efi subsystem with the firmware subsystem
  1369. * and the vars subsystem with the efi subsystem. In the future, it
  1370. * might make sense to split off the efi subsystem into its own
  1371. * driver, but for now only efivars will register with it, so just
  1372. * include it here.
  1373. */
  1374. static int __init
  1375. efivars_init(void)
  1376. {
  1377. int error = 0;
  1378. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1379. EFIVARS_DATE);
  1380. if (!efi_enabled)
  1381. return 0;
  1382. /* For now we'll register the efi directory at /sys/firmware/efi */
  1383. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1384. if (!efi_kobj) {
  1385. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1386. return -ENOMEM;
  1387. }
  1388. ops.get_variable = efi.get_variable;
  1389. ops.set_variable = efi.set_variable;
  1390. ops.get_next_variable = efi.get_next_variable;
  1391. error = register_efivars(&__efivars, &ops, efi_kobj);
  1392. if (error)
  1393. goto err_put;
  1394. /* Don't forget the systab entry */
  1395. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1396. if (error) {
  1397. printk(KERN_ERR
  1398. "efivars: Sysfs attribute export failed with error %d.\n",
  1399. error);
  1400. goto err_unregister;
  1401. }
  1402. return 0;
  1403. err_unregister:
  1404. unregister_efivars(&__efivars);
  1405. err_put:
  1406. kobject_put(efi_kobj);
  1407. return error;
  1408. }
  1409. static void __exit
  1410. efivars_exit(void)
  1411. {
  1412. if (efi_enabled) {
  1413. unregister_efivars(&__efivars);
  1414. kobject_put(efi_kobj);
  1415. }
  1416. }
  1417. module_init(efivars_init);
  1418. module_exit(efivars_exit);