efivars.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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. /*
  93. * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
  94. * not including trailing NUL
  95. */
  96. #define GUID_LEN 36
  97. /*
  98. * The maximum size of VariableName + Data = 1024
  99. * Therefore, it's reasonable to save that much
  100. * space in each part of the structure,
  101. * and we use a page for reading/writing.
  102. */
  103. struct efi_variable {
  104. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  105. efi_guid_t VendorGuid;
  106. unsigned long DataSize;
  107. __u8 Data[1024];
  108. efi_status_t Status;
  109. __u32 Attributes;
  110. } __attribute__((packed));
  111. struct efivar_entry {
  112. struct efivars *efivars;
  113. struct efi_variable var;
  114. struct list_head list;
  115. struct kobject kobj;
  116. };
  117. struct efivar_attribute {
  118. struct attribute attr;
  119. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  120. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  121. };
  122. static struct efivars __efivars;
  123. static struct efivar_operations ops;
  124. #define PSTORE_EFI_ATTRIBUTES \
  125. (EFI_VARIABLE_NON_VOLATILE | \
  126. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  127. EFI_VARIABLE_RUNTIME_ACCESS)
  128. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  129. struct efivar_attribute efivar_attr_##_name = { \
  130. .attr = {.name = __stringify(_name), .mode = _mode}, \
  131. .show = _show, \
  132. .store = _store, \
  133. };
  134. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  135. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  136. /*
  137. * Prototype for sysfs creation function
  138. */
  139. static int
  140. efivar_create_sysfs_entry(struct efivars *efivars,
  141. unsigned long variable_name_size,
  142. efi_char16_t *variable_name,
  143. efi_guid_t *vendor_guid);
  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. spin_lock(&efivars->lock);
  350. status = get_var_data_locked(efivars, var);
  351. spin_unlock(&efivars->lock);
  352. if (status != EFI_SUCCESS) {
  353. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  354. status);
  355. }
  356. return status;
  357. }
  358. static ssize_t
  359. efivar_guid_read(struct efivar_entry *entry, char *buf)
  360. {
  361. struct efi_variable *var = &entry->var;
  362. char *str = buf;
  363. if (!entry || !buf)
  364. return 0;
  365. efi_guid_unparse(&var->VendorGuid, str);
  366. str += strlen(str);
  367. str += sprintf(str, "\n");
  368. return str - buf;
  369. }
  370. static ssize_t
  371. efivar_attr_read(struct efivar_entry *entry, char *buf)
  372. {
  373. struct efi_variable *var = &entry->var;
  374. char *str = buf;
  375. efi_status_t status;
  376. if (!entry || !buf)
  377. return -EINVAL;
  378. status = get_var_data(entry->efivars, var);
  379. if (status != EFI_SUCCESS)
  380. return -EIO;
  381. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  382. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  383. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  384. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  385. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  386. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  387. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  388. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  389. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  390. str += sprintf(str,
  391. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  392. if (var->Attributes &
  393. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  394. str += sprintf(str,
  395. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  396. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  397. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  398. return str - buf;
  399. }
  400. static ssize_t
  401. efivar_size_read(struct efivar_entry *entry, char *buf)
  402. {
  403. struct efi_variable *var = &entry->var;
  404. char *str = buf;
  405. efi_status_t status;
  406. if (!entry || !buf)
  407. return -EINVAL;
  408. status = get_var_data(entry->efivars, var);
  409. if (status != EFI_SUCCESS)
  410. return -EIO;
  411. str += sprintf(str, "0x%lx\n", var->DataSize);
  412. return str - buf;
  413. }
  414. static ssize_t
  415. efivar_data_read(struct efivar_entry *entry, char *buf)
  416. {
  417. struct efi_variable *var = &entry->var;
  418. efi_status_t status;
  419. if (!entry || !buf)
  420. return -EINVAL;
  421. status = get_var_data(entry->efivars, var);
  422. if (status != EFI_SUCCESS)
  423. return -EIO;
  424. memcpy(buf, var->Data, var->DataSize);
  425. return var->DataSize;
  426. }
  427. /*
  428. * We allow each variable to be edited via rewriting the
  429. * entire efi variable structure.
  430. */
  431. static ssize_t
  432. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  433. {
  434. struct efi_variable *new_var, *var = &entry->var;
  435. struct efivars *efivars = entry->efivars;
  436. efi_status_t status = EFI_NOT_FOUND;
  437. if (count != sizeof(struct efi_variable))
  438. return -EINVAL;
  439. new_var = (struct efi_variable *)buf;
  440. /*
  441. * If only updating the variable data, then the name
  442. * and guid should remain the same
  443. */
  444. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  445. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  446. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  447. return -EINVAL;
  448. }
  449. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  450. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  451. return -EINVAL;
  452. }
  453. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  454. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  455. printk(KERN_ERR "efivars: Malformed variable content\n");
  456. return -EINVAL;
  457. }
  458. spin_lock(&efivars->lock);
  459. status = efivars->ops->set_variable(new_var->VariableName,
  460. &new_var->VendorGuid,
  461. new_var->Attributes,
  462. new_var->DataSize,
  463. new_var->Data);
  464. spin_unlock(&efivars->lock);
  465. if (status != EFI_SUCCESS) {
  466. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  467. status);
  468. return -EIO;
  469. }
  470. memcpy(&entry->var, new_var, count);
  471. return count;
  472. }
  473. static ssize_t
  474. efivar_show_raw(struct efivar_entry *entry, char *buf)
  475. {
  476. struct efi_variable *var = &entry->var;
  477. efi_status_t status;
  478. if (!entry || !buf)
  479. return 0;
  480. status = get_var_data(entry->efivars, var);
  481. if (status != EFI_SUCCESS)
  482. return -EIO;
  483. memcpy(buf, var, sizeof(*var));
  484. return sizeof(*var);
  485. }
  486. /*
  487. * Generic read/write functions that call the specific functions of
  488. * the attributes...
  489. */
  490. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  491. char *buf)
  492. {
  493. struct efivar_entry *var = to_efivar_entry(kobj);
  494. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  495. ssize_t ret = -EIO;
  496. if (!capable(CAP_SYS_ADMIN))
  497. return -EACCES;
  498. if (efivar_attr->show) {
  499. ret = efivar_attr->show(var, buf);
  500. }
  501. return ret;
  502. }
  503. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  504. const char *buf, size_t count)
  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->store)
  512. ret = efivar_attr->store(var, buf, count);
  513. return ret;
  514. }
  515. static const struct sysfs_ops efivar_attr_ops = {
  516. .show = efivar_attr_show,
  517. .store = efivar_attr_store,
  518. };
  519. static void efivar_release(struct kobject *kobj)
  520. {
  521. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  522. kfree(var);
  523. }
  524. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  525. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  526. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  527. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  528. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  529. static struct attribute *def_attrs[] = {
  530. &efivar_attr_guid.attr,
  531. &efivar_attr_size.attr,
  532. &efivar_attr_attributes.attr,
  533. &efivar_attr_data.attr,
  534. &efivar_attr_raw_var.attr,
  535. NULL,
  536. };
  537. static struct kobj_type efivar_ktype = {
  538. .release = efivar_release,
  539. .sysfs_ops = &efivar_attr_ops,
  540. .default_attrs = def_attrs,
  541. };
  542. static inline void
  543. efivar_unregister(struct efivar_entry *var)
  544. {
  545. kobject_put(&var->kobj);
  546. }
  547. static int efivarfs_file_open(struct inode *inode, struct file *file)
  548. {
  549. file->private_data = inode->i_private;
  550. return 0;
  551. }
  552. static int efi_status_to_err(efi_status_t status)
  553. {
  554. int err;
  555. switch (status) {
  556. case EFI_INVALID_PARAMETER:
  557. err = -EINVAL;
  558. break;
  559. case EFI_OUT_OF_RESOURCES:
  560. err = -ENOSPC;
  561. break;
  562. case EFI_DEVICE_ERROR:
  563. err = -EIO;
  564. break;
  565. case EFI_WRITE_PROTECTED:
  566. err = -EROFS;
  567. break;
  568. case EFI_SECURITY_VIOLATION:
  569. err = -EACCES;
  570. break;
  571. case EFI_NOT_FOUND:
  572. err = -EIO;
  573. break;
  574. default:
  575. err = -EINVAL;
  576. }
  577. return err;
  578. }
  579. static ssize_t efivarfs_file_write(struct file *file,
  580. const char __user *userbuf, size_t count, loff_t *ppos)
  581. {
  582. struct efivar_entry *var = file->private_data;
  583. struct efivars *efivars;
  584. efi_status_t status;
  585. void *data;
  586. u32 attributes;
  587. struct inode *inode = file->f_mapping->host;
  588. unsigned long datasize = count - sizeof(attributes);
  589. unsigned long newdatasize;
  590. u64 storage_size, remaining_size, max_size;
  591. ssize_t bytes = 0;
  592. if (count < sizeof(attributes))
  593. return -EINVAL;
  594. if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
  595. return -EFAULT;
  596. if (attributes & ~(EFI_VARIABLE_MASK))
  597. return -EINVAL;
  598. efivars = var->efivars;
  599. /*
  600. * Ensure that the user can't allocate arbitrarily large
  601. * amounts of memory. Pick a default size of 64K if
  602. * QueryVariableInfo() isn't supported by the firmware.
  603. */
  604. spin_lock(&efivars->lock);
  605. if (!efivars->ops->query_variable_info)
  606. status = EFI_UNSUPPORTED;
  607. else {
  608. const struct efivar_operations *fops = efivars->ops;
  609. status = fops->query_variable_info(attributes, &storage_size,
  610. &remaining_size, &max_size);
  611. }
  612. spin_unlock(&efivars->lock);
  613. if (status != EFI_SUCCESS) {
  614. if (status != EFI_UNSUPPORTED)
  615. return efi_status_to_err(status);
  616. remaining_size = 65536;
  617. }
  618. if (datasize > remaining_size)
  619. return -ENOSPC;
  620. data = kmalloc(datasize, GFP_KERNEL);
  621. if (!data)
  622. return -ENOMEM;
  623. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  624. bytes = -EFAULT;
  625. goto out;
  626. }
  627. if (validate_var(&var->var, data, datasize) == false) {
  628. bytes = -EINVAL;
  629. goto out;
  630. }
  631. /*
  632. * The lock here protects the get_variable call, the conditional
  633. * set_variable call, and removal of the variable from the efivars
  634. * list (in the case of an authenticated delete).
  635. */
  636. spin_lock(&efivars->lock);
  637. status = efivars->ops->set_variable(var->var.VariableName,
  638. &var->var.VendorGuid,
  639. attributes, datasize,
  640. data);
  641. if (status != EFI_SUCCESS) {
  642. spin_unlock(&efivars->lock);
  643. kfree(data);
  644. return efi_status_to_err(status);
  645. }
  646. bytes = count;
  647. /*
  648. * Writing to the variable may have caused a change in size (which
  649. * could either be an append or an overwrite), or the variable to be
  650. * deleted. Perform a GetVariable() so we can tell what actually
  651. * happened.
  652. */
  653. newdatasize = 0;
  654. status = efivars->ops->get_variable(var->var.VariableName,
  655. &var->var.VendorGuid,
  656. NULL, &newdatasize,
  657. NULL);
  658. if (status == EFI_BUFFER_TOO_SMALL) {
  659. spin_unlock(&efivars->lock);
  660. mutex_lock(&inode->i_mutex);
  661. i_size_write(inode, newdatasize + sizeof(attributes));
  662. mutex_unlock(&inode->i_mutex);
  663. } else if (status == EFI_NOT_FOUND) {
  664. list_del(&var->list);
  665. spin_unlock(&efivars->lock);
  666. efivar_unregister(var);
  667. drop_nlink(inode);
  668. d_delete(file->f_dentry);
  669. dput(file->f_dentry);
  670. } else {
  671. spin_unlock(&efivars->lock);
  672. pr_warn("efivarfs: inconsistent EFI variable implementation? "
  673. "status = %lx\n", status);
  674. }
  675. out:
  676. kfree(data);
  677. return bytes;
  678. }
  679. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  680. size_t count, loff_t *ppos)
  681. {
  682. struct efivar_entry *var = file->private_data;
  683. struct efivars *efivars = var->efivars;
  684. efi_status_t status;
  685. unsigned long datasize = 0;
  686. u32 attributes;
  687. void *data;
  688. ssize_t size = 0;
  689. spin_lock(&efivars->lock);
  690. status = efivars->ops->get_variable(var->var.VariableName,
  691. &var->var.VendorGuid,
  692. &attributes, &datasize, NULL);
  693. spin_unlock(&efivars->lock);
  694. if (status != EFI_BUFFER_TOO_SMALL)
  695. return efi_status_to_err(status);
  696. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  697. if (!data)
  698. return -ENOMEM;
  699. spin_lock(&efivars->lock);
  700. status = efivars->ops->get_variable(var->var.VariableName,
  701. &var->var.VendorGuid,
  702. &attributes, &datasize,
  703. (data + sizeof(attributes)));
  704. spin_unlock(&efivars->lock);
  705. if (status != EFI_SUCCESS) {
  706. size = efi_status_to_err(status);
  707. goto out_free;
  708. }
  709. memcpy(data, &attributes, sizeof(attributes));
  710. size = simple_read_from_buffer(userbuf, count, ppos,
  711. data, datasize + sizeof(attributes));
  712. out_free:
  713. kfree(data);
  714. return size;
  715. }
  716. static void efivarfs_evict_inode(struct inode *inode)
  717. {
  718. clear_inode(inode);
  719. }
  720. static const struct super_operations efivarfs_ops = {
  721. .statfs = simple_statfs,
  722. .drop_inode = generic_delete_inode,
  723. .evict_inode = efivarfs_evict_inode,
  724. .show_options = generic_show_options,
  725. };
  726. static struct super_block *efivarfs_sb;
  727. static const struct inode_operations efivarfs_dir_inode_operations;
  728. static const struct file_operations efivarfs_file_operations = {
  729. .open = efivarfs_file_open,
  730. .read = efivarfs_file_read,
  731. .write = efivarfs_file_write,
  732. .llseek = no_llseek,
  733. };
  734. static struct inode *efivarfs_get_inode(struct super_block *sb,
  735. const struct inode *dir, int mode, dev_t dev)
  736. {
  737. struct inode *inode = new_inode(sb);
  738. if (inode) {
  739. inode->i_ino = get_next_ino();
  740. inode->i_mode = mode;
  741. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  742. switch (mode & S_IFMT) {
  743. case S_IFREG:
  744. inode->i_fop = &efivarfs_file_operations;
  745. break;
  746. case S_IFDIR:
  747. inode->i_op = &efivarfs_dir_inode_operations;
  748. inode->i_fop = &simple_dir_operations;
  749. inc_nlink(inode);
  750. break;
  751. }
  752. }
  753. return inode;
  754. }
  755. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  756. {
  757. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  758. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  759. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  760. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  761. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  762. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  763. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  764. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  765. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  766. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  767. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  768. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  769. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  770. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  771. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  772. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  773. }
  774. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  775. umode_t mode, bool excl)
  776. {
  777. struct inode *inode;
  778. struct efivars *efivars = &__efivars;
  779. struct efivar_entry *var;
  780. int namelen, i = 0, err = 0;
  781. /*
  782. * We need a GUID, plus at least one letter for the variable name,
  783. * plus the '-' separator
  784. */
  785. if (dentry->d_name.len < GUID_LEN + 2)
  786. return -EINVAL;
  787. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  788. if (!inode)
  789. return -ENOMEM;
  790. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  791. if (!var) {
  792. err = -ENOMEM;
  793. goto out;
  794. }
  795. /* length of the variable name itself: remove GUID and separator */
  796. namelen = dentry->d_name.len - GUID_LEN - 1;
  797. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  798. &var->var.VendorGuid);
  799. for (i = 0; i < namelen; i++)
  800. var->var.VariableName[i] = dentry->d_name.name[i];
  801. var->var.VariableName[i] = '\0';
  802. inode->i_private = var;
  803. var->efivars = efivars;
  804. var->kobj.kset = efivars->kset;
  805. err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
  806. dentry->d_name.name);
  807. if (err)
  808. goto out;
  809. kobject_uevent(&var->kobj, KOBJ_ADD);
  810. spin_lock(&efivars->lock);
  811. list_add(&var->list, &efivars->list);
  812. spin_unlock(&efivars->lock);
  813. d_instantiate(dentry, inode);
  814. dget(dentry);
  815. out:
  816. if (err) {
  817. kfree(var);
  818. iput(inode);
  819. }
  820. return err;
  821. }
  822. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  823. {
  824. struct efivar_entry *var = dentry->d_inode->i_private;
  825. struct efivars *efivars = var->efivars;
  826. efi_status_t status;
  827. spin_lock(&efivars->lock);
  828. status = efivars->ops->set_variable(var->var.VariableName,
  829. &var->var.VendorGuid,
  830. 0, 0, NULL);
  831. if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
  832. list_del(&var->list);
  833. spin_unlock(&efivars->lock);
  834. efivar_unregister(var);
  835. drop_nlink(dentry->d_inode);
  836. dput(dentry);
  837. return 0;
  838. }
  839. spin_unlock(&efivars->lock);
  840. return -EINVAL;
  841. };
  842. static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  843. {
  844. struct inode *inode = NULL;
  845. struct dentry *root;
  846. struct efivar_entry *entry, *n;
  847. struct efivars *efivars = &__efivars;
  848. char *name;
  849. efivarfs_sb = sb;
  850. sb->s_maxbytes = MAX_LFS_FILESIZE;
  851. sb->s_blocksize = PAGE_CACHE_SIZE;
  852. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  853. sb->s_magic = EFIVARFS_MAGIC;
  854. sb->s_op = &efivarfs_ops;
  855. sb->s_time_gran = 1;
  856. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  857. if (!inode)
  858. return -ENOMEM;
  859. inode->i_op = &efivarfs_dir_inode_operations;
  860. root = d_make_root(inode);
  861. sb->s_root = root;
  862. if (!root)
  863. return -ENOMEM;
  864. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  865. struct dentry *dentry, *root = efivarfs_sb->s_root;
  866. unsigned long size = 0;
  867. int len, i;
  868. inode = NULL;
  869. len = utf16_strlen(entry->var.VariableName);
  870. /* name, plus '-', plus GUID, plus NUL*/
  871. name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
  872. if (!name)
  873. goto fail;
  874. for (i = 0; i < len; i++)
  875. name[i] = entry->var.VariableName[i] & 0xFF;
  876. name[len] = '-';
  877. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  878. name[len+GUID_LEN+1] = '\0';
  879. inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
  880. S_IFREG | 0644, 0);
  881. if (!inode)
  882. goto fail_name;
  883. dentry = d_alloc_name(root, name);
  884. if (!dentry)
  885. goto fail_inode;
  886. /* copied by the above to local storage in the dentry. */
  887. kfree(name);
  888. spin_lock(&efivars->lock);
  889. efivars->ops->get_variable(entry->var.VariableName,
  890. &entry->var.VendorGuid,
  891. &entry->var.Attributes,
  892. &size,
  893. NULL);
  894. spin_unlock(&efivars->lock);
  895. mutex_lock(&inode->i_mutex);
  896. inode->i_private = entry;
  897. i_size_write(inode, size+4);
  898. mutex_unlock(&inode->i_mutex);
  899. d_add(dentry, inode);
  900. }
  901. return 0;
  902. fail_inode:
  903. iput(inode);
  904. fail_name:
  905. kfree(name);
  906. fail:
  907. return -ENOMEM;
  908. }
  909. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  910. int flags, const char *dev_name, void *data)
  911. {
  912. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  913. }
  914. static void efivarfs_kill_sb(struct super_block *sb)
  915. {
  916. kill_litter_super(sb);
  917. efivarfs_sb = NULL;
  918. }
  919. static struct file_system_type efivarfs_type = {
  920. .name = "efivarfs",
  921. .mount = efivarfs_mount,
  922. .kill_sb = efivarfs_kill_sb,
  923. };
  924. static const struct inode_operations efivarfs_dir_inode_operations = {
  925. .lookup = simple_lookup,
  926. .unlink = efivarfs_unlink,
  927. .create = efivarfs_create,
  928. };
  929. static struct pstore_info efi_pstore_info;
  930. #ifdef CONFIG_PSTORE
  931. static int efi_pstore_open(struct pstore_info *psi)
  932. {
  933. struct efivars *efivars = psi->data;
  934. spin_lock(&efivars->lock);
  935. efivars->walk_entry = list_first_entry(&efivars->list,
  936. struct efivar_entry, list);
  937. return 0;
  938. }
  939. static int efi_pstore_close(struct pstore_info *psi)
  940. {
  941. struct efivars *efivars = psi->data;
  942. spin_unlock(&efivars->lock);
  943. return 0;
  944. }
  945. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  946. int *count, struct timespec *timespec,
  947. char **buf, struct pstore_info *psi)
  948. {
  949. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  950. struct efivars *efivars = psi->data;
  951. char name[DUMP_NAME_LEN];
  952. int i;
  953. int cnt;
  954. unsigned int part, size;
  955. unsigned long time;
  956. while (&efivars->walk_entry->list != &efivars->list) {
  957. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  958. vendor)) {
  959. for (i = 0; i < DUMP_NAME_LEN; i++) {
  960. name[i] = efivars->walk_entry->var.VariableName[i];
  961. }
  962. if (sscanf(name, "dump-type%u-%u-%d-%lu",
  963. type, &part, &cnt, &time) == 4) {
  964. *id = part;
  965. *count = cnt;
  966. timespec->tv_sec = time;
  967. timespec->tv_nsec = 0;
  968. } else if (sscanf(name, "dump-type%u-%u-%lu",
  969. type, &part, &time) == 3) {
  970. /*
  971. * Check if an old format,
  972. * which doesn't support holding
  973. * multiple logs, remains.
  974. */
  975. *id = part;
  976. *count = 0;
  977. timespec->tv_sec = time;
  978. timespec->tv_nsec = 0;
  979. } else {
  980. efivars->walk_entry = list_entry(
  981. efivars->walk_entry->list.next,
  982. struct efivar_entry, list);
  983. continue;
  984. }
  985. get_var_data_locked(efivars, &efivars->walk_entry->var);
  986. size = efivars->walk_entry->var.DataSize;
  987. *buf = kmalloc(size, GFP_KERNEL);
  988. if (*buf == NULL)
  989. return -ENOMEM;
  990. memcpy(*buf, efivars->walk_entry->var.Data,
  991. size);
  992. efivars->walk_entry = list_entry(
  993. efivars->walk_entry->list.next,
  994. struct efivar_entry, list);
  995. return size;
  996. }
  997. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  998. struct efivar_entry, list);
  999. }
  1000. return 0;
  1001. }
  1002. static int efi_pstore_write(enum pstore_type_id type,
  1003. enum kmsg_dump_reason reason, u64 *id,
  1004. unsigned int part, int count, size_t size,
  1005. struct pstore_info *psi)
  1006. {
  1007. char name[DUMP_NAME_LEN];
  1008. efi_char16_t efi_name[DUMP_NAME_LEN];
  1009. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1010. struct efivars *efivars = psi->data;
  1011. int i, ret = 0;
  1012. u64 storage_space, remaining_space, max_variable_size;
  1013. efi_status_t status = EFI_NOT_FOUND;
  1014. spin_lock(&efivars->lock);
  1015. /*
  1016. * Check if there is a space enough to log.
  1017. * size: a size of logging data
  1018. * DUMP_NAME_LEN * 2: a maximum size of variable name
  1019. */
  1020. status = efivars->ops->query_variable_info(PSTORE_EFI_ATTRIBUTES,
  1021. &storage_space,
  1022. &remaining_space,
  1023. &max_variable_size);
  1024. if (status || remaining_space < size + DUMP_NAME_LEN * 2) {
  1025. spin_unlock(&efivars->lock);
  1026. *id = part;
  1027. return -ENOSPC;
  1028. }
  1029. sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
  1030. get_seconds());
  1031. for (i = 0; i < DUMP_NAME_LEN; i++)
  1032. efi_name[i] = name[i];
  1033. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  1034. size, psi->buf);
  1035. spin_unlock(&efivars->lock);
  1036. if (size)
  1037. ret = efivar_create_sysfs_entry(efivars,
  1038. utf16_strsize(efi_name,
  1039. DUMP_NAME_LEN * 2),
  1040. efi_name, &vendor);
  1041. *id = part;
  1042. return ret;
  1043. };
  1044. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  1045. struct timespec time, struct pstore_info *psi)
  1046. {
  1047. char name[DUMP_NAME_LEN];
  1048. efi_char16_t efi_name[DUMP_NAME_LEN];
  1049. char name_old[DUMP_NAME_LEN];
  1050. efi_char16_t efi_name_old[DUMP_NAME_LEN];
  1051. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1052. struct efivars *efivars = psi->data;
  1053. struct efivar_entry *entry, *found = NULL;
  1054. int i;
  1055. sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
  1056. time.tv_sec);
  1057. spin_lock(&efivars->lock);
  1058. for (i = 0; i < DUMP_NAME_LEN; i++)
  1059. efi_name[i] = name[i];
  1060. /*
  1061. * Clean up an entry with the same name
  1062. */
  1063. list_for_each_entry(entry, &efivars->list, list) {
  1064. get_var_data_locked(efivars, &entry->var);
  1065. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  1066. continue;
  1067. if (utf16_strncmp(entry->var.VariableName, efi_name,
  1068. utf16_strlen(efi_name))) {
  1069. /*
  1070. * Check if an old format,
  1071. * which doesn't support holding
  1072. * multiple logs, remains.
  1073. */
  1074. sprintf(name_old, "dump-type%u-%u-%lu", type,
  1075. (unsigned int)id, time.tv_sec);
  1076. for (i = 0; i < DUMP_NAME_LEN; i++)
  1077. efi_name_old[i] = name_old[i];
  1078. if (utf16_strncmp(entry->var.VariableName, efi_name_old,
  1079. utf16_strlen(efi_name_old)))
  1080. continue;
  1081. }
  1082. /* found */
  1083. found = entry;
  1084. efivars->ops->set_variable(entry->var.VariableName,
  1085. &entry->var.VendorGuid,
  1086. PSTORE_EFI_ATTRIBUTES,
  1087. 0, NULL);
  1088. break;
  1089. }
  1090. if (found)
  1091. list_del(&found->list);
  1092. spin_unlock(&efivars->lock);
  1093. if (found)
  1094. efivar_unregister(found);
  1095. return 0;
  1096. }
  1097. #else
  1098. static int efi_pstore_open(struct pstore_info *psi)
  1099. {
  1100. return 0;
  1101. }
  1102. static int efi_pstore_close(struct pstore_info *psi)
  1103. {
  1104. return 0;
  1105. }
  1106. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, int *count,
  1107. struct timespec *timespec,
  1108. char **buf, struct pstore_info *psi)
  1109. {
  1110. return -1;
  1111. }
  1112. static int efi_pstore_write(enum pstore_type_id type,
  1113. enum kmsg_dump_reason reason, u64 *id,
  1114. unsigned int part, int count, size_t size,
  1115. struct pstore_info *psi)
  1116. {
  1117. return 0;
  1118. }
  1119. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  1120. struct timespec time, struct pstore_info *psi)
  1121. {
  1122. return 0;
  1123. }
  1124. #endif
  1125. static struct pstore_info efi_pstore_info = {
  1126. .owner = THIS_MODULE,
  1127. .name = "efi",
  1128. .open = efi_pstore_open,
  1129. .close = efi_pstore_close,
  1130. .read = efi_pstore_read,
  1131. .write = efi_pstore_write,
  1132. .erase = efi_pstore_erase,
  1133. };
  1134. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  1135. struct bin_attribute *bin_attr,
  1136. char *buf, loff_t pos, size_t count)
  1137. {
  1138. struct efi_variable *new_var = (struct efi_variable *)buf;
  1139. struct efivars *efivars = bin_attr->private;
  1140. struct efivar_entry *search_efivar, *n;
  1141. unsigned long strsize1, strsize2;
  1142. efi_status_t status = EFI_NOT_FOUND;
  1143. int found = 0;
  1144. if (!capable(CAP_SYS_ADMIN))
  1145. return -EACCES;
  1146. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  1147. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  1148. printk(KERN_ERR "efivars: Malformed variable content\n");
  1149. return -EINVAL;
  1150. }
  1151. spin_lock(&efivars->lock);
  1152. /*
  1153. * Does this variable already exist?
  1154. */
  1155. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1156. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1157. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  1158. if (strsize1 == strsize2 &&
  1159. !memcmp(&(search_efivar->var.VariableName),
  1160. new_var->VariableName, strsize1) &&
  1161. !efi_guidcmp(search_efivar->var.VendorGuid,
  1162. new_var->VendorGuid)) {
  1163. found = 1;
  1164. break;
  1165. }
  1166. }
  1167. if (found) {
  1168. spin_unlock(&efivars->lock);
  1169. return -EINVAL;
  1170. }
  1171. /* now *really* create the variable via EFI */
  1172. status = efivars->ops->set_variable(new_var->VariableName,
  1173. &new_var->VendorGuid,
  1174. new_var->Attributes,
  1175. new_var->DataSize,
  1176. new_var->Data);
  1177. if (status != EFI_SUCCESS) {
  1178. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1179. status);
  1180. spin_unlock(&efivars->lock);
  1181. return -EIO;
  1182. }
  1183. spin_unlock(&efivars->lock);
  1184. /* Create the entry in sysfs. Locking is not required here */
  1185. status = efivar_create_sysfs_entry(efivars,
  1186. utf16_strsize(new_var->VariableName,
  1187. 1024),
  1188. new_var->VariableName,
  1189. &new_var->VendorGuid);
  1190. if (status) {
  1191. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  1192. }
  1193. return count;
  1194. }
  1195. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  1196. struct bin_attribute *bin_attr,
  1197. char *buf, loff_t pos, size_t count)
  1198. {
  1199. struct efi_variable *del_var = (struct efi_variable *)buf;
  1200. struct efivars *efivars = bin_attr->private;
  1201. struct efivar_entry *search_efivar, *n;
  1202. unsigned long strsize1, strsize2;
  1203. efi_status_t status = EFI_NOT_FOUND;
  1204. int found = 0;
  1205. if (!capable(CAP_SYS_ADMIN))
  1206. return -EACCES;
  1207. spin_lock(&efivars->lock);
  1208. /*
  1209. * Does this variable already exist?
  1210. */
  1211. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1212. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1213. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  1214. if (strsize1 == strsize2 &&
  1215. !memcmp(&(search_efivar->var.VariableName),
  1216. del_var->VariableName, strsize1) &&
  1217. !efi_guidcmp(search_efivar->var.VendorGuid,
  1218. del_var->VendorGuid)) {
  1219. found = 1;
  1220. break;
  1221. }
  1222. }
  1223. if (!found) {
  1224. spin_unlock(&efivars->lock);
  1225. return -EINVAL;
  1226. }
  1227. /* force the Attributes/DataSize to 0 to ensure deletion */
  1228. del_var->Attributes = 0;
  1229. del_var->DataSize = 0;
  1230. status = efivars->ops->set_variable(del_var->VariableName,
  1231. &del_var->VendorGuid,
  1232. del_var->Attributes,
  1233. del_var->DataSize,
  1234. del_var->Data);
  1235. if (status != EFI_SUCCESS) {
  1236. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1237. status);
  1238. spin_unlock(&efivars->lock);
  1239. return -EIO;
  1240. }
  1241. list_del(&search_efivar->list);
  1242. /* We need to release this lock before unregistering. */
  1243. spin_unlock(&efivars->lock);
  1244. efivar_unregister(search_efivar);
  1245. /* It's dead Jim.... */
  1246. return count;
  1247. }
  1248. /*
  1249. * Let's not leave out systab information that snuck into
  1250. * the efivars driver
  1251. */
  1252. static ssize_t systab_show(struct kobject *kobj,
  1253. struct kobj_attribute *attr, char *buf)
  1254. {
  1255. char *str = buf;
  1256. if (!kobj || !buf)
  1257. return -EINVAL;
  1258. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1259. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1260. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1261. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1262. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1263. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1264. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1265. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1266. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1267. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1268. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1269. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1270. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1271. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1272. return str - buf;
  1273. }
  1274. static struct kobj_attribute efi_attr_systab =
  1275. __ATTR(systab, 0400, systab_show, NULL);
  1276. static struct attribute *efi_subsys_attrs[] = {
  1277. &efi_attr_systab.attr,
  1278. NULL, /* maybe more in the future? */
  1279. };
  1280. static struct attribute_group efi_subsys_attr_group = {
  1281. .attrs = efi_subsys_attrs,
  1282. };
  1283. static struct kobject *efi_kobj;
  1284. /*
  1285. * efivar_create_sysfs_entry()
  1286. * Requires:
  1287. * variable_name_size = number of bytes required to hold
  1288. * variable_name (not counting the NULL
  1289. * character at the end.
  1290. * efivars->lock is not held on entry or exit.
  1291. * Returns 1 on failure, 0 on success
  1292. */
  1293. static int
  1294. efivar_create_sysfs_entry(struct efivars *efivars,
  1295. unsigned long variable_name_size,
  1296. efi_char16_t *variable_name,
  1297. efi_guid_t *vendor_guid)
  1298. {
  1299. int i, short_name_size;
  1300. char *short_name;
  1301. struct efivar_entry *new_efivar;
  1302. /*
  1303. * Length of the variable bytes in ASCII, plus the '-' separator,
  1304. * plus the GUID, plus trailing NUL
  1305. */
  1306. short_name_size = variable_name_size / sizeof(efi_char16_t)
  1307. + 1 + GUID_LEN + 1;
  1308. short_name = kzalloc(short_name_size, GFP_KERNEL);
  1309. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  1310. if (!short_name || !new_efivar) {
  1311. kfree(short_name);
  1312. kfree(new_efivar);
  1313. return 1;
  1314. }
  1315. new_efivar->efivars = efivars;
  1316. memcpy(new_efivar->var.VariableName, variable_name,
  1317. variable_name_size);
  1318. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1319. /* Convert Unicode to normal chars (assume top bits are 0),
  1320. ala UTF-8 */
  1321. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1322. short_name[i] = variable_name[i] & 0xFF;
  1323. }
  1324. /* This is ugly, but necessary to separate one vendor's
  1325. private variables from another's. */
  1326. *(short_name + strlen(short_name)) = '-';
  1327. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1328. new_efivar->kobj.kset = efivars->kset;
  1329. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1330. "%s", short_name);
  1331. if (i) {
  1332. kfree(short_name);
  1333. kfree(new_efivar);
  1334. return 1;
  1335. }
  1336. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1337. kfree(short_name);
  1338. short_name = NULL;
  1339. spin_lock(&efivars->lock);
  1340. list_add(&new_efivar->list, &efivars->list);
  1341. spin_unlock(&efivars->lock);
  1342. return 0;
  1343. }
  1344. static int
  1345. create_efivars_bin_attributes(struct efivars *efivars)
  1346. {
  1347. struct bin_attribute *attr;
  1348. int error;
  1349. /* new_var */
  1350. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1351. if (!attr)
  1352. return -ENOMEM;
  1353. attr->attr.name = "new_var";
  1354. attr->attr.mode = 0200;
  1355. attr->write = efivar_create;
  1356. attr->private = efivars;
  1357. efivars->new_var = attr;
  1358. /* del_var */
  1359. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1360. if (!attr) {
  1361. error = -ENOMEM;
  1362. goto out_free;
  1363. }
  1364. attr->attr.name = "del_var";
  1365. attr->attr.mode = 0200;
  1366. attr->write = efivar_delete;
  1367. attr->private = efivars;
  1368. efivars->del_var = attr;
  1369. sysfs_bin_attr_init(efivars->new_var);
  1370. sysfs_bin_attr_init(efivars->del_var);
  1371. /* Register */
  1372. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1373. efivars->new_var);
  1374. if (error) {
  1375. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1376. " due to error %d\n", error);
  1377. goto out_free;
  1378. }
  1379. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1380. efivars->del_var);
  1381. if (error) {
  1382. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1383. " due to error %d\n", error);
  1384. sysfs_remove_bin_file(&efivars->kset->kobj,
  1385. efivars->new_var);
  1386. goto out_free;
  1387. }
  1388. return 0;
  1389. out_free:
  1390. kfree(efivars->del_var);
  1391. efivars->del_var = NULL;
  1392. kfree(efivars->new_var);
  1393. efivars->new_var = NULL;
  1394. return error;
  1395. }
  1396. void unregister_efivars(struct efivars *efivars)
  1397. {
  1398. struct efivar_entry *entry, *n;
  1399. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1400. spin_lock(&efivars->lock);
  1401. list_del(&entry->list);
  1402. spin_unlock(&efivars->lock);
  1403. efivar_unregister(entry);
  1404. }
  1405. if (efivars->new_var)
  1406. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1407. if (efivars->del_var)
  1408. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1409. kfree(efivars->new_var);
  1410. kfree(efivars->del_var);
  1411. kobject_put(efivars->kobject);
  1412. kset_unregister(efivars->kset);
  1413. }
  1414. EXPORT_SYMBOL_GPL(unregister_efivars);
  1415. int register_efivars(struct efivars *efivars,
  1416. const struct efivar_operations *ops,
  1417. struct kobject *parent_kobj)
  1418. {
  1419. efi_status_t status = EFI_NOT_FOUND;
  1420. efi_guid_t vendor_guid;
  1421. efi_char16_t *variable_name;
  1422. unsigned long variable_name_size = 1024;
  1423. int error = 0;
  1424. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1425. if (!variable_name) {
  1426. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1427. return -ENOMEM;
  1428. }
  1429. spin_lock_init(&efivars->lock);
  1430. INIT_LIST_HEAD(&efivars->list);
  1431. efivars->ops = ops;
  1432. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1433. if (!efivars->kset) {
  1434. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1435. error = -ENOMEM;
  1436. goto out;
  1437. }
  1438. efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
  1439. if (!efivars->kobject) {
  1440. pr_err("efivars: Subsystem registration failed.\n");
  1441. error = -ENOMEM;
  1442. kset_unregister(efivars->kset);
  1443. goto out;
  1444. }
  1445. /*
  1446. * Per EFI spec, the maximum storage allocated for both
  1447. * the variable name and variable data is 1024 bytes.
  1448. */
  1449. do {
  1450. variable_name_size = 1024;
  1451. status = ops->get_next_variable(&variable_name_size,
  1452. variable_name,
  1453. &vendor_guid);
  1454. switch (status) {
  1455. case EFI_SUCCESS:
  1456. efivar_create_sysfs_entry(efivars,
  1457. variable_name_size,
  1458. variable_name,
  1459. &vendor_guid);
  1460. break;
  1461. case EFI_NOT_FOUND:
  1462. break;
  1463. default:
  1464. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1465. status);
  1466. status = EFI_NOT_FOUND;
  1467. break;
  1468. }
  1469. } while (status != EFI_NOT_FOUND);
  1470. error = create_efivars_bin_attributes(efivars);
  1471. if (error)
  1472. unregister_efivars(efivars);
  1473. efivars->efi_pstore_info = efi_pstore_info;
  1474. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1475. if (efivars->efi_pstore_info.buf) {
  1476. efivars->efi_pstore_info.bufsize = 1024;
  1477. efivars->efi_pstore_info.data = efivars;
  1478. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1479. pstore_register(&efivars->efi_pstore_info);
  1480. }
  1481. register_filesystem(&efivarfs_type);
  1482. out:
  1483. kfree(variable_name);
  1484. return error;
  1485. }
  1486. EXPORT_SYMBOL_GPL(register_efivars);
  1487. /*
  1488. * For now we register the efi subsystem with the firmware subsystem
  1489. * and the vars subsystem with the efi subsystem. In the future, it
  1490. * might make sense to split off the efi subsystem into its own
  1491. * driver, but for now only efivars will register with it, so just
  1492. * include it here.
  1493. */
  1494. static int __init
  1495. efivars_init(void)
  1496. {
  1497. int error = 0;
  1498. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1499. EFIVARS_DATE);
  1500. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  1501. return 0;
  1502. /* For now we'll register the efi directory at /sys/firmware/efi */
  1503. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1504. if (!efi_kobj) {
  1505. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1506. return -ENOMEM;
  1507. }
  1508. ops.get_variable = efi.get_variable;
  1509. ops.set_variable = efi.set_variable;
  1510. ops.get_next_variable = efi.get_next_variable;
  1511. ops.query_variable_info = efi.query_variable_info;
  1512. error = register_efivars(&__efivars, &ops, efi_kobj);
  1513. if (error)
  1514. goto err_put;
  1515. /* Don't forget the systab entry */
  1516. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1517. if (error) {
  1518. printk(KERN_ERR
  1519. "efivars: Sysfs attribute export failed with error %d.\n",
  1520. error);
  1521. goto err_unregister;
  1522. }
  1523. return 0;
  1524. err_unregister:
  1525. unregister_efivars(&__efivars);
  1526. err_put:
  1527. kobject_put(efi_kobj);
  1528. return error;
  1529. }
  1530. static void __exit
  1531. efivars_exit(void)
  1532. {
  1533. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  1534. unregister_efivars(&__efivars);
  1535. kobject_put(efi_kobj);
  1536. }
  1537. }
  1538. module_init(efivars_init);
  1539. module_exit(efivars_exit);