efivars.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  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/ctype.h>
  82. #include <linux/fs.h>
  83. #include <linux/ramfs.h>
  84. #include <linux/pagemap.h>
  85. #include <asm/uaccess.h>
  86. #define EFIVARS_VERSION "0.08"
  87. #define EFIVARS_DATE "2004-May-17"
  88. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  89. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  90. MODULE_LICENSE("GPL");
  91. MODULE_VERSION(EFIVARS_VERSION);
  92. #define DUMP_NAME_LEN 52
  93. /*
  94. * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
  95. * not including trailing NUL
  96. */
  97. #define GUID_LEN 36
  98. static bool efivars_pstore_disable =
  99. IS_ENABLED(EFI_VARS_PSTORE_DEFAULT_DISABLE);
  100. module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
  101. /*
  102. * The maximum size of VariableName + Data = 1024
  103. * Therefore, it's reasonable to save that much
  104. * space in each part of the structure,
  105. * and we use a page for reading/writing.
  106. */
  107. struct efi_variable {
  108. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  109. efi_guid_t VendorGuid;
  110. unsigned long DataSize;
  111. __u8 Data[1024];
  112. efi_status_t Status;
  113. __u32 Attributes;
  114. } __attribute__((packed));
  115. struct efivar_entry {
  116. struct efivars *efivars;
  117. struct efi_variable var;
  118. struct list_head list;
  119. struct kobject kobj;
  120. };
  121. struct efivar_attribute {
  122. struct attribute attr;
  123. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  124. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  125. };
  126. static struct efivars __efivars;
  127. static struct efivar_operations ops;
  128. #define PSTORE_EFI_ATTRIBUTES \
  129. (EFI_VARIABLE_NON_VOLATILE | \
  130. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  131. EFI_VARIABLE_RUNTIME_ACCESS)
  132. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  133. struct efivar_attribute efivar_attr_##_name = { \
  134. .attr = {.name = __stringify(_name), .mode = _mode}, \
  135. .show = _show, \
  136. .store = _store, \
  137. };
  138. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  139. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  140. /*
  141. * Prototype for sysfs creation function
  142. */
  143. static int
  144. efivar_create_sysfs_entry(struct efivars *efivars,
  145. unsigned long variable_name_size,
  146. efi_char16_t *variable_name,
  147. efi_guid_t *vendor_guid);
  148. /*
  149. * Prototype for workqueue functions updating sysfs entry
  150. */
  151. static void efivar_update_sysfs_entries(struct work_struct *);
  152. static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
  153. /* Return the number of unicode characters in data */
  154. static unsigned long
  155. utf16_strnlen(efi_char16_t *s, size_t maxlength)
  156. {
  157. unsigned long length = 0;
  158. while (*s++ != 0 && length < maxlength)
  159. length++;
  160. return length;
  161. }
  162. static inline unsigned long
  163. utf16_strlen(efi_char16_t *s)
  164. {
  165. return utf16_strnlen(s, ~0UL);
  166. }
  167. /*
  168. * Return the number of bytes is the length of this string
  169. * Note: this is NOT the same as the number of unicode characters
  170. */
  171. static inline unsigned long
  172. utf16_strsize(efi_char16_t *data, unsigned long maxlength)
  173. {
  174. return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
  175. }
  176. static inline int
  177. utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
  178. {
  179. while (1) {
  180. if (len == 0)
  181. return 0;
  182. if (*a < *b)
  183. return -1;
  184. if (*a > *b)
  185. return 1;
  186. if (*a == 0) /* implies *b == 0 */
  187. return 0;
  188. a++;
  189. b++;
  190. len--;
  191. }
  192. }
  193. static bool
  194. validate_device_path(struct efi_variable *var, int match, u8 *buffer,
  195. unsigned long len)
  196. {
  197. struct efi_generic_dev_path *node;
  198. int offset = 0;
  199. node = (struct efi_generic_dev_path *)buffer;
  200. if (len < sizeof(*node))
  201. return false;
  202. while (offset <= len - sizeof(*node) &&
  203. node->length >= sizeof(*node) &&
  204. node->length <= len - offset) {
  205. offset += node->length;
  206. if ((node->type == EFI_DEV_END_PATH ||
  207. node->type == EFI_DEV_END_PATH2) &&
  208. node->sub_type == EFI_DEV_END_ENTIRE)
  209. return true;
  210. node = (struct efi_generic_dev_path *)(buffer + offset);
  211. }
  212. /*
  213. * If we're here then either node->length pointed past the end
  214. * of the buffer or we reached the end of the buffer without
  215. * finding a device path end node.
  216. */
  217. return false;
  218. }
  219. static bool
  220. validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
  221. unsigned long len)
  222. {
  223. /* An array of 16-bit integers */
  224. if ((len % 2) != 0)
  225. return false;
  226. return true;
  227. }
  228. static bool
  229. validate_load_option(struct efi_variable *var, int match, u8 *buffer,
  230. unsigned long len)
  231. {
  232. u16 filepathlength;
  233. int i, desclength = 0, namelen;
  234. namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
  235. /* Either "Boot" or "Driver" followed by four digits of hex */
  236. for (i = match; i < match+4; i++) {
  237. if (var->VariableName[i] > 127 ||
  238. hex_to_bin(var->VariableName[i] & 0xff) < 0)
  239. return true;
  240. }
  241. /* Reject it if there's 4 digits of hex and then further content */
  242. if (namelen > match + 4)
  243. return false;
  244. /* A valid entry must be at least 8 bytes */
  245. if (len < 8)
  246. return false;
  247. filepathlength = buffer[4] | buffer[5] << 8;
  248. /*
  249. * There's no stored length for the description, so it has to be
  250. * found by hand
  251. */
  252. desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  253. /* Each boot entry must have a descriptor */
  254. if (!desclength)
  255. return false;
  256. /*
  257. * If the sum of the length of the description, the claimed filepath
  258. * length and the original header are greater than the length of the
  259. * variable, it's malformed
  260. */
  261. if ((desclength + filepathlength + 6) > len)
  262. return false;
  263. /*
  264. * And, finally, check the filepath
  265. */
  266. return validate_device_path(var, match, buffer + desclength + 6,
  267. filepathlength);
  268. }
  269. static bool
  270. validate_uint16(struct efi_variable *var, int match, u8 *buffer,
  271. unsigned long len)
  272. {
  273. /* A single 16-bit integer */
  274. if (len != 2)
  275. return false;
  276. return true;
  277. }
  278. static bool
  279. validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
  280. unsigned long len)
  281. {
  282. int i;
  283. for (i = 0; i < len; i++) {
  284. if (buffer[i] > 127)
  285. return false;
  286. if (buffer[i] == 0)
  287. return true;
  288. }
  289. return false;
  290. }
  291. struct variable_validate {
  292. char *name;
  293. bool (*validate)(struct efi_variable *var, int match, u8 *data,
  294. unsigned long len);
  295. };
  296. static const struct variable_validate variable_validate[] = {
  297. { "BootNext", validate_uint16 },
  298. { "BootOrder", validate_boot_order },
  299. { "DriverOrder", validate_boot_order },
  300. { "Boot*", validate_load_option },
  301. { "Driver*", validate_load_option },
  302. { "ConIn", validate_device_path },
  303. { "ConInDev", validate_device_path },
  304. { "ConOut", validate_device_path },
  305. { "ConOutDev", validate_device_path },
  306. { "ErrOut", validate_device_path },
  307. { "ErrOutDev", validate_device_path },
  308. { "Timeout", validate_uint16 },
  309. { "Lang", validate_ascii_string },
  310. { "PlatformLang", validate_ascii_string },
  311. { "", NULL },
  312. };
  313. static bool
  314. validate_var(struct efi_variable *var, u8 *data, unsigned long len)
  315. {
  316. int i;
  317. u16 *unicode_name = var->VariableName;
  318. for (i = 0; variable_validate[i].validate != NULL; i++) {
  319. const char *name = variable_validate[i].name;
  320. int match;
  321. for (match = 0; ; match++) {
  322. char c = name[match];
  323. u16 u = unicode_name[match];
  324. /* All special variables are plain ascii */
  325. if (u > 127)
  326. return true;
  327. /* Wildcard in the matching name means we've matched */
  328. if (c == '*')
  329. return variable_validate[i].validate(var,
  330. match, data, len);
  331. /* Case sensitive match */
  332. if (c != u)
  333. break;
  334. /* Reached the end of the string while matching */
  335. if (!c)
  336. return variable_validate[i].validate(var,
  337. match, data, len);
  338. }
  339. }
  340. return true;
  341. }
  342. static efi_status_t
  343. get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
  344. {
  345. efi_status_t status;
  346. var->DataSize = 1024;
  347. status = efivars->ops->get_variable(var->VariableName,
  348. &var->VendorGuid,
  349. &var->Attributes,
  350. &var->DataSize,
  351. var->Data);
  352. return status;
  353. }
  354. static efi_status_t
  355. get_var_data(struct efivars *efivars, struct efi_variable *var)
  356. {
  357. efi_status_t status;
  358. unsigned long flags;
  359. spin_lock_irqsave(&efivars->lock, flags);
  360. status = get_var_data_locked(efivars, var);
  361. spin_unlock_irqrestore(&efivars->lock, flags);
  362. if (status != EFI_SUCCESS) {
  363. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  364. status);
  365. }
  366. return status;
  367. }
  368. static efi_status_t
  369. check_var_size_locked(struct efivars *efivars, u32 attributes,
  370. unsigned long size)
  371. {
  372. u64 storage_size, remaining_size, max_size;
  373. efi_status_t status;
  374. const struct efivar_operations *fops = efivars->ops;
  375. if (!efivars->ops->query_variable_info)
  376. return EFI_UNSUPPORTED;
  377. status = fops->query_variable_info(attributes, &storage_size,
  378. &remaining_size, &max_size);
  379. if (status != EFI_SUCCESS)
  380. return status;
  381. if (!storage_size || size > remaining_size || size > max_size ||
  382. (remaining_size - size) < (storage_size / 2))
  383. return EFI_OUT_OF_RESOURCES;
  384. return status;
  385. }
  386. static efi_status_t
  387. check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
  388. {
  389. efi_status_t status;
  390. unsigned long flags;
  391. spin_lock_irqsave(&efivars->lock, flags);
  392. status = check_var_size_locked(efivars, attributes, size);
  393. spin_unlock_irqrestore(&efivars->lock, flags);
  394. return status;
  395. }
  396. static ssize_t
  397. efivar_guid_read(struct efivar_entry *entry, char *buf)
  398. {
  399. struct efi_variable *var = &entry->var;
  400. char *str = buf;
  401. if (!entry || !buf)
  402. return 0;
  403. efi_guid_unparse(&var->VendorGuid, str);
  404. str += strlen(str);
  405. str += sprintf(str, "\n");
  406. return str - buf;
  407. }
  408. static ssize_t
  409. efivar_attr_read(struct efivar_entry *entry, char *buf)
  410. {
  411. struct efi_variable *var = &entry->var;
  412. char *str = buf;
  413. efi_status_t status;
  414. if (!entry || !buf)
  415. return -EINVAL;
  416. status = get_var_data(entry->efivars, var);
  417. if (status != EFI_SUCCESS)
  418. return -EIO;
  419. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  420. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  421. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  422. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  423. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  424. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  425. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  426. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  427. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  428. str += sprintf(str,
  429. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  430. if (var->Attributes &
  431. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  432. str += sprintf(str,
  433. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  434. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  435. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  436. return str - buf;
  437. }
  438. static ssize_t
  439. efivar_size_read(struct efivar_entry *entry, char *buf)
  440. {
  441. struct efi_variable *var = &entry->var;
  442. char *str = buf;
  443. efi_status_t status;
  444. if (!entry || !buf)
  445. return -EINVAL;
  446. status = get_var_data(entry->efivars, var);
  447. if (status != EFI_SUCCESS)
  448. return -EIO;
  449. str += sprintf(str, "0x%lx\n", var->DataSize);
  450. return str - buf;
  451. }
  452. static ssize_t
  453. efivar_data_read(struct efivar_entry *entry, char *buf)
  454. {
  455. struct efi_variable *var = &entry->var;
  456. efi_status_t status;
  457. if (!entry || !buf)
  458. return -EINVAL;
  459. status = get_var_data(entry->efivars, var);
  460. if (status != EFI_SUCCESS)
  461. return -EIO;
  462. memcpy(buf, var->Data, var->DataSize);
  463. return var->DataSize;
  464. }
  465. /*
  466. * We allow each variable to be edited via rewriting the
  467. * entire efi variable structure.
  468. */
  469. static ssize_t
  470. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  471. {
  472. struct efi_variable *new_var, *var = &entry->var;
  473. struct efivars *efivars = entry->efivars;
  474. efi_status_t status = EFI_NOT_FOUND;
  475. if (count != sizeof(struct efi_variable))
  476. return -EINVAL;
  477. new_var = (struct efi_variable *)buf;
  478. /*
  479. * If only updating the variable data, then the name
  480. * and guid should remain the same
  481. */
  482. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  483. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  484. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  485. return -EINVAL;
  486. }
  487. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  488. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  489. return -EINVAL;
  490. }
  491. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  492. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  493. printk(KERN_ERR "efivars: Malformed variable content\n");
  494. return -EINVAL;
  495. }
  496. spin_lock_irq(&efivars->lock);
  497. status = check_var_size_locked(efivars, new_var->Attributes,
  498. new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
  499. if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
  500. status = efivars->ops->set_variable(new_var->VariableName,
  501. &new_var->VendorGuid,
  502. new_var->Attributes,
  503. new_var->DataSize,
  504. new_var->Data);
  505. spin_unlock_irq(&efivars->lock);
  506. if (status != EFI_SUCCESS) {
  507. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  508. status);
  509. return -EIO;
  510. }
  511. memcpy(&entry->var, new_var, count);
  512. return count;
  513. }
  514. static ssize_t
  515. efivar_show_raw(struct efivar_entry *entry, char *buf)
  516. {
  517. struct efi_variable *var = &entry->var;
  518. efi_status_t status;
  519. if (!entry || !buf)
  520. return 0;
  521. status = get_var_data(entry->efivars, var);
  522. if (status != EFI_SUCCESS)
  523. return -EIO;
  524. memcpy(buf, var, sizeof(*var));
  525. return sizeof(*var);
  526. }
  527. /*
  528. * Generic read/write functions that call the specific functions of
  529. * the attributes...
  530. */
  531. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  532. char *buf)
  533. {
  534. struct efivar_entry *var = to_efivar_entry(kobj);
  535. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  536. ssize_t ret = -EIO;
  537. if (!capable(CAP_SYS_ADMIN))
  538. return -EACCES;
  539. if (efivar_attr->show) {
  540. ret = efivar_attr->show(var, buf);
  541. }
  542. return ret;
  543. }
  544. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  545. const char *buf, size_t count)
  546. {
  547. struct efivar_entry *var = to_efivar_entry(kobj);
  548. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  549. ssize_t ret = -EIO;
  550. if (!capable(CAP_SYS_ADMIN))
  551. return -EACCES;
  552. if (efivar_attr->store)
  553. ret = efivar_attr->store(var, buf, count);
  554. return ret;
  555. }
  556. static const struct sysfs_ops efivar_attr_ops = {
  557. .show = efivar_attr_show,
  558. .store = efivar_attr_store,
  559. };
  560. static void efivar_release(struct kobject *kobj)
  561. {
  562. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  563. kfree(var);
  564. }
  565. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  566. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  567. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  568. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  569. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  570. static struct attribute *def_attrs[] = {
  571. &efivar_attr_guid.attr,
  572. &efivar_attr_size.attr,
  573. &efivar_attr_attributes.attr,
  574. &efivar_attr_data.attr,
  575. &efivar_attr_raw_var.attr,
  576. NULL,
  577. };
  578. static struct kobj_type efivar_ktype = {
  579. .release = efivar_release,
  580. .sysfs_ops = &efivar_attr_ops,
  581. .default_attrs = def_attrs,
  582. };
  583. static inline void
  584. efivar_unregister(struct efivar_entry *var)
  585. {
  586. kobject_put(&var->kobj);
  587. }
  588. static int efivarfs_file_open(struct inode *inode, struct file *file)
  589. {
  590. file->private_data = inode->i_private;
  591. return 0;
  592. }
  593. static int efi_status_to_err(efi_status_t status)
  594. {
  595. int err;
  596. switch (status) {
  597. case EFI_INVALID_PARAMETER:
  598. err = -EINVAL;
  599. break;
  600. case EFI_OUT_OF_RESOURCES:
  601. err = -ENOSPC;
  602. break;
  603. case EFI_DEVICE_ERROR:
  604. err = -EIO;
  605. break;
  606. case EFI_WRITE_PROTECTED:
  607. err = -EROFS;
  608. break;
  609. case EFI_SECURITY_VIOLATION:
  610. err = -EACCES;
  611. break;
  612. case EFI_NOT_FOUND:
  613. err = -EIO;
  614. break;
  615. default:
  616. err = -EINVAL;
  617. }
  618. return err;
  619. }
  620. static ssize_t efivarfs_file_write(struct file *file,
  621. const char __user *userbuf, size_t count, loff_t *ppos)
  622. {
  623. struct efivar_entry *var = file->private_data;
  624. struct efivars *efivars;
  625. efi_status_t status;
  626. void *data;
  627. u32 attributes;
  628. struct inode *inode = file->f_mapping->host;
  629. unsigned long datasize = count - sizeof(attributes);
  630. unsigned long newdatasize, varsize;
  631. ssize_t bytes = 0;
  632. if (count < sizeof(attributes))
  633. return -EINVAL;
  634. if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
  635. return -EFAULT;
  636. if (attributes & ~(EFI_VARIABLE_MASK))
  637. return -EINVAL;
  638. efivars = var->efivars;
  639. /*
  640. * Ensure that the user can't allocate arbitrarily large
  641. * amounts of memory. Pick a default size of 64K if
  642. * QueryVariableInfo() isn't supported by the firmware.
  643. */
  644. varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
  645. status = check_var_size(efivars, attributes, varsize);
  646. if (status != EFI_SUCCESS) {
  647. if (status != EFI_UNSUPPORTED)
  648. return efi_status_to_err(status);
  649. if (datasize > 65536)
  650. return -ENOSPC;
  651. }
  652. data = kmalloc(datasize, GFP_KERNEL);
  653. if (!data)
  654. return -ENOMEM;
  655. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  656. bytes = -EFAULT;
  657. goto out;
  658. }
  659. if (validate_var(&var->var, data, datasize) == false) {
  660. bytes = -EINVAL;
  661. goto out;
  662. }
  663. /*
  664. * The lock here protects the get_variable call, the conditional
  665. * set_variable call, and removal of the variable from the efivars
  666. * list (in the case of an authenticated delete).
  667. */
  668. spin_lock_irq(&efivars->lock);
  669. /*
  670. * Ensure that the available space hasn't shrunk below the safe level
  671. */
  672. status = check_var_size_locked(efivars, attributes, varsize);
  673. if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
  674. spin_unlock_irq(&efivars->lock);
  675. kfree(data);
  676. return efi_status_to_err(status);
  677. }
  678. status = efivars->ops->set_variable(var->var.VariableName,
  679. &var->var.VendorGuid,
  680. attributes, datasize,
  681. data);
  682. if (status != EFI_SUCCESS) {
  683. spin_unlock_irq(&efivars->lock);
  684. kfree(data);
  685. return efi_status_to_err(status);
  686. }
  687. bytes = count;
  688. /*
  689. * Writing to the variable may have caused a change in size (which
  690. * could either be an append or an overwrite), or the variable to be
  691. * deleted. Perform a GetVariable() so we can tell what actually
  692. * happened.
  693. */
  694. newdatasize = 0;
  695. status = efivars->ops->get_variable(var->var.VariableName,
  696. &var->var.VendorGuid,
  697. NULL, &newdatasize,
  698. NULL);
  699. if (status == EFI_BUFFER_TOO_SMALL) {
  700. spin_unlock_irq(&efivars->lock);
  701. mutex_lock(&inode->i_mutex);
  702. i_size_write(inode, newdatasize + sizeof(attributes));
  703. mutex_unlock(&inode->i_mutex);
  704. } else if (status == EFI_NOT_FOUND) {
  705. list_del(&var->list);
  706. spin_unlock_irq(&efivars->lock);
  707. efivar_unregister(var);
  708. drop_nlink(inode);
  709. d_delete(file->f_dentry);
  710. dput(file->f_dentry);
  711. } else {
  712. spin_unlock_irq(&efivars->lock);
  713. pr_warn("efivarfs: inconsistent EFI variable implementation? "
  714. "status = %lx\n", status);
  715. }
  716. out:
  717. kfree(data);
  718. return bytes;
  719. }
  720. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  721. size_t count, loff_t *ppos)
  722. {
  723. struct efivar_entry *var = file->private_data;
  724. struct efivars *efivars = var->efivars;
  725. efi_status_t status;
  726. unsigned long datasize = 0;
  727. u32 attributes;
  728. void *data;
  729. ssize_t size = 0;
  730. spin_lock_irq(&efivars->lock);
  731. status = efivars->ops->get_variable(var->var.VariableName,
  732. &var->var.VendorGuid,
  733. &attributes, &datasize, NULL);
  734. spin_unlock_irq(&efivars->lock);
  735. if (status != EFI_BUFFER_TOO_SMALL)
  736. return efi_status_to_err(status);
  737. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  738. if (!data)
  739. return -ENOMEM;
  740. spin_lock_irq(&efivars->lock);
  741. status = efivars->ops->get_variable(var->var.VariableName,
  742. &var->var.VendorGuid,
  743. &attributes, &datasize,
  744. (data + sizeof(attributes)));
  745. spin_unlock_irq(&efivars->lock);
  746. if (status != EFI_SUCCESS) {
  747. size = efi_status_to_err(status);
  748. goto out_free;
  749. }
  750. memcpy(data, &attributes, sizeof(attributes));
  751. size = simple_read_from_buffer(userbuf, count, ppos,
  752. data, datasize + sizeof(attributes));
  753. out_free:
  754. kfree(data);
  755. return size;
  756. }
  757. static void efivarfs_evict_inode(struct inode *inode)
  758. {
  759. clear_inode(inode);
  760. }
  761. static const struct super_operations efivarfs_ops = {
  762. .statfs = simple_statfs,
  763. .drop_inode = generic_delete_inode,
  764. .evict_inode = efivarfs_evict_inode,
  765. .show_options = generic_show_options,
  766. };
  767. static struct super_block *efivarfs_sb;
  768. static const struct inode_operations efivarfs_dir_inode_operations;
  769. static const struct file_operations efivarfs_file_operations = {
  770. .open = efivarfs_file_open,
  771. .read = efivarfs_file_read,
  772. .write = efivarfs_file_write,
  773. .llseek = no_llseek,
  774. };
  775. static struct inode *efivarfs_get_inode(struct super_block *sb,
  776. const struct inode *dir, int mode, dev_t dev)
  777. {
  778. struct inode *inode = new_inode(sb);
  779. if (inode) {
  780. inode->i_ino = get_next_ino();
  781. inode->i_mode = mode;
  782. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  783. switch (mode & S_IFMT) {
  784. case S_IFREG:
  785. inode->i_fop = &efivarfs_file_operations;
  786. break;
  787. case S_IFDIR:
  788. inode->i_op = &efivarfs_dir_inode_operations;
  789. inode->i_fop = &simple_dir_operations;
  790. inc_nlink(inode);
  791. break;
  792. }
  793. }
  794. return inode;
  795. }
  796. /*
  797. * Return true if 'str' is a valid efivarfs filename of the form,
  798. *
  799. * VariableName-12345678-1234-1234-1234-1234567891bc
  800. */
  801. static bool efivarfs_valid_name(const char *str, int len)
  802. {
  803. static const char dashes[GUID_LEN] = {
  804. [8] = 1, [13] = 1, [18] = 1, [23] = 1
  805. };
  806. const char *s = str + len - GUID_LEN;
  807. int i;
  808. /*
  809. * We need a GUID, plus at least one letter for the variable name,
  810. * plus the '-' separator
  811. */
  812. if (len < GUID_LEN + 2)
  813. return false;
  814. /* GUID must be preceded by a '-' */
  815. if (*(s - 1) != '-')
  816. return false;
  817. /*
  818. * Validate that 's' is of the correct format, e.g.
  819. *
  820. * 12345678-1234-1234-1234-123456789abc
  821. */
  822. for (i = 0; i < GUID_LEN; i++) {
  823. if (dashes[i]) {
  824. if (*s++ != '-')
  825. return false;
  826. } else {
  827. if (!isxdigit(*s++))
  828. return false;
  829. }
  830. }
  831. return true;
  832. }
  833. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  834. {
  835. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  836. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  837. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  838. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  839. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  840. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  841. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  842. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  843. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  844. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  845. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  846. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  847. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  848. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  849. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  850. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  851. }
  852. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  853. umode_t mode, bool excl)
  854. {
  855. struct inode *inode;
  856. struct efivars *efivars = &__efivars;
  857. struct efivar_entry *var;
  858. int namelen, i = 0, err = 0;
  859. if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
  860. return -EINVAL;
  861. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  862. if (!inode)
  863. return -ENOMEM;
  864. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  865. if (!var) {
  866. err = -ENOMEM;
  867. goto out;
  868. }
  869. /* length of the variable name itself: remove GUID and separator */
  870. namelen = dentry->d_name.len - GUID_LEN - 1;
  871. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  872. &var->var.VendorGuid);
  873. for (i = 0; i < namelen; i++)
  874. var->var.VariableName[i] = dentry->d_name.name[i];
  875. var->var.VariableName[i] = '\0';
  876. inode->i_private = var;
  877. var->efivars = efivars;
  878. var->kobj.kset = efivars->kset;
  879. err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
  880. dentry->d_name.name);
  881. if (err)
  882. goto out;
  883. kobject_uevent(&var->kobj, KOBJ_ADD);
  884. spin_lock_irq(&efivars->lock);
  885. list_add(&var->list, &efivars->list);
  886. spin_unlock_irq(&efivars->lock);
  887. d_instantiate(dentry, inode);
  888. dget(dentry);
  889. out:
  890. if (err) {
  891. kfree(var);
  892. iput(inode);
  893. }
  894. return err;
  895. }
  896. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  897. {
  898. struct efivar_entry *var = dentry->d_inode->i_private;
  899. struct efivars *efivars = var->efivars;
  900. efi_status_t status;
  901. spin_lock_irq(&efivars->lock);
  902. status = efivars->ops->set_variable(var->var.VariableName,
  903. &var->var.VendorGuid,
  904. 0, 0, NULL);
  905. if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
  906. list_del(&var->list);
  907. spin_unlock_irq(&efivars->lock);
  908. efivar_unregister(var);
  909. drop_nlink(dentry->d_inode);
  910. dput(dentry);
  911. return 0;
  912. }
  913. spin_unlock_irq(&efivars->lock);
  914. return -EINVAL;
  915. };
  916. /*
  917. * Compare two efivarfs file names.
  918. *
  919. * An efivarfs filename is composed of two parts,
  920. *
  921. * 1. A case-sensitive variable name
  922. * 2. A case-insensitive GUID
  923. *
  924. * So we need to perform a case-sensitive match on part 1 and a
  925. * case-insensitive match on part 2.
  926. */
  927. static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
  928. const struct dentry *dentry, const struct inode *inode,
  929. unsigned int len, const char *str,
  930. const struct qstr *name)
  931. {
  932. int guid = len - GUID_LEN;
  933. if (name->len != len)
  934. return 1;
  935. /* Case-sensitive compare for the variable name */
  936. if (memcmp(str, name->name, guid))
  937. return 1;
  938. /* Case-insensitive compare for the GUID */
  939. return strncasecmp(name->name + guid, str + guid, GUID_LEN);
  940. }
  941. static int efivarfs_d_hash(const struct dentry *dentry,
  942. const struct inode *inode, struct qstr *qstr)
  943. {
  944. unsigned long hash = init_name_hash();
  945. const unsigned char *s = qstr->name;
  946. unsigned int len = qstr->len;
  947. if (!efivarfs_valid_name(s, len))
  948. return -EINVAL;
  949. while (len-- > GUID_LEN)
  950. hash = partial_name_hash(*s++, hash);
  951. /* GUID is case-insensitive. */
  952. while (len--)
  953. hash = partial_name_hash(tolower(*s++), hash);
  954. qstr->hash = end_name_hash(hash);
  955. return 0;
  956. }
  957. /*
  958. * Retaining negative dentries for an in-memory filesystem just wastes
  959. * memory and lookup time: arrange for them to be deleted immediately.
  960. */
  961. static int efivarfs_delete_dentry(const struct dentry *dentry)
  962. {
  963. return 1;
  964. }
  965. static struct dentry_operations efivarfs_d_ops = {
  966. .d_compare = efivarfs_d_compare,
  967. .d_hash = efivarfs_d_hash,
  968. .d_delete = efivarfs_delete_dentry,
  969. };
  970. static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
  971. {
  972. struct dentry *d;
  973. struct qstr q;
  974. int err;
  975. q.name = name;
  976. q.len = strlen(name);
  977. err = efivarfs_d_hash(NULL, NULL, &q);
  978. if (err)
  979. return ERR_PTR(err);
  980. d = d_alloc(parent, &q);
  981. if (d)
  982. return d;
  983. return ERR_PTR(-ENOMEM);
  984. }
  985. static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  986. {
  987. struct inode *inode = NULL;
  988. struct dentry *root;
  989. struct efivar_entry *entry, *n;
  990. struct efivars *efivars = &__efivars;
  991. char *name;
  992. int err = -ENOMEM;
  993. efivarfs_sb = sb;
  994. sb->s_maxbytes = MAX_LFS_FILESIZE;
  995. sb->s_blocksize = PAGE_CACHE_SIZE;
  996. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  997. sb->s_magic = EFIVARFS_MAGIC;
  998. sb->s_op = &efivarfs_ops;
  999. sb->s_d_op = &efivarfs_d_ops;
  1000. sb->s_time_gran = 1;
  1001. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  1002. if (!inode)
  1003. return -ENOMEM;
  1004. inode->i_op = &efivarfs_dir_inode_operations;
  1005. root = d_make_root(inode);
  1006. sb->s_root = root;
  1007. if (!root)
  1008. return -ENOMEM;
  1009. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1010. struct dentry *dentry, *root = efivarfs_sb->s_root;
  1011. unsigned long size = 0;
  1012. int len, i;
  1013. inode = NULL;
  1014. len = utf16_strlen(entry->var.VariableName);
  1015. /* name, plus '-', plus GUID, plus NUL*/
  1016. name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
  1017. if (!name)
  1018. goto fail;
  1019. for (i = 0; i < len; i++)
  1020. name[i] = entry->var.VariableName[i] & 0xFF;
  1021. name[len] = '-';
  1022. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  1023. name[len+GUID_LEN+1] = '\0';
  1024. inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
  1025. S_IFREG | 0644, 0);
  1026. if (!inode)
  1027. goto fail_name;
  1028. dentry = efivarfs_alloc_dentry(root, name);
  1029. if (IS_ERR(dentry)) {
  1030. err = PTR_ERR(dentry);
  1031. goto fail_inode;
  1032. }
  1033. /* copied by the above to local storage in the dentry. */
  1034. kfree(name);
  1035. spin_lock_irq(&efivars->lock);
  1036. efivars->ops->get_variable(entry->var.VariableName,
  1037. &entry->var.VendorGuid,
  1038. &entry->var.Attributes,
  1039. &size,
  1040. NULL);
  1041. spin_unlock_irq(&efivars->lock);
  1042. mutex_lock(&inode->i_mutex);
  1043. inode->i_private = entry;
  1044. i_size_write(inode, size + sizeof(entry->var.Attributes));
  1045. mutex_unlock(&inode->i_mutex);
  1046. d_add(dentry, inode);
  1047. }
  1048. return 0;
  1049. fail_inode:
  1050. iput(inode);
  1051. fail_name:
  1052. kfree(name);
  1053. fail:
  1054. return err;
  1055. }
  1056. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  1057. int flags, const char *dev_name, void *data)
  1058. {
  1059. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  1060. }
  1061. static void efivarfs_kill_sb(struct super_block *sb)
  1062. {
  1063. kill_litter_super(sb);
  1064. efivarfs_sb = NULL;
  1065. }
  1066. static struct file_system_type efivarfs_type = {
  1067. .name = "efivarfs",
  1068. .mount = efivarfs_mount,
  1069. .kill_sb = efivarfs_kill_sb,
  1070. };
  1071. MODULE_ALIAS_FS("efivarfs");
  1072. /*
  1073. * Handle negative dentry.
  1074. */
  1075. static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
  1076. unsigned int flags)
  1077. {
  1078. if (dentry->d_name.len > NAME_MAX)
  1079. return ERR_PTR(-ENAMETOOLONG);
  1080. d_add(dentry, NULL);
  1081. return NULL;
  1082. }
  1083. static const struct inode_operations efivarfs_dir_inode_operations = {
  1084. .lookup = efivarfs_lookup,
  1085. .unlink = efivarfs_unlink,
  1086. .create = efivarfs_create,
  1087. };
  1088. #ifdef CONFIG_EFI_VARS_PSTORE
  1089. static int efi_pstore_open(struct pstore_info *psi)
  1090. {
  1091. struct efivars *efivars = psi->data;
  1092. spin_lock_irq(&efivars->lock);
  1093. efivars->walk_entry = list_first_entry(&efivars->list,
  1094. struct efivar_entry, list);
  1095. return 0;
  1096. }
  1097. static int efi_pstore_close(struct pstore_info *psi)
  1098. {
  1099. struct efivars *efivars = psi->data;
  1100. spin_unlock_irq(&efivars->lock);
  1101. return 0;
  1102. }
  1103. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  1104. int *count, struct timespec *timespec,
  1105. char **buf, struct pstore_info *psi)
  1106. {
  1107. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1108. struct efivars *efivars = psi->data;
  1109. char name[DUMP_NAME_LEN];
  1110. int i;
  1111. int cnt;
  1112. unsigned int part, size;
  1113. unsigned long time;
  1114. while (&efivars->walk_entry->list != &efivars->list) {
  1115. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  1116. vendor)) {
  1117. for (i = 0; i < DUMP_NAME_LEN; i++) {
  1118. name[i] = efivars->walk_entry->var.VariableName[i];
  1119. }
  1120. if (sscanf(name, "dump-type%u-%u-%d-%lu",
  1121. type, &part, &cnt, &time) == 4) {
  1122. *id = part;
  1123. *count = cnt;
  1124. timespec->tv_sec = time;
  1125. timespec->tv_nsec = 0;
  1126. } else if (sscanf(name, "dump-type%u-%u-%lu",
  1127. type, &part, &time) == 3) {
  1128. /*
  1129. * Check if an old format,
  1130. * which doesn't support holding
  1131. * multiple logs, remains.
  1132. */
  1133. *id = part;
  1134. *count = 0;
  1135. timespec->tv_sec = time;
  1136. timespec->tv_nsec = 0;
  1137. } else {
  1138. efivars->walk_entry = list_entry(
  1139. efivars->walk_entry->list.next,
  1140. struct efivar_entry, list);
  1141. continue;
  1142. }
  1143. get_var_data_locked(efivars, &efivars->walk_entry->var);
  1144. size = efivars->walk_entry->var.DataSize;
  1145. *buf = kmalloc(size, GFP_KERNEL);
  1146. if (*buf == NULL)
  1147. return -ENOMEM;
  1148. memcpy(*buf, efivars->walk_entry->var.Data,
  1149. size);
  1150. efivars->walk_entry = list_entry(
  1151. efivars->walk_entry->list.next,
  1152. struct efivar_entry, list);
  1153. return size;
  1154. }
  1155. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  1156. struct efivar_entry, list);
  1157. }
  1158. return 0;
  1159. }
  1160. static int efi_pstore_write(enum pstore_type_id type,
  1161. enum kmsg_dump_reason reason, u64 *id,
  1162. unsigned int part, int count, size_t size,
  1163. struct pstore_info *psi)
  1164. {
  1165. char name[DUMP_NAME_LEN];
  1166. efi_char16_t efi_name[DUMP_NAME_LEN];
  1167. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1168. struct efivars *efivars = psi->data;
  1169. int i, ret = 0;
  1170. efi_status_t status = EFI_NOT_FOUND;
  1171. unsigned long flags;
  1172. if (pstore_cannot_block_path(reason)) {
  1173. /*
  1174. * If the lock is taken by another cpu in non-blocking path,
  1175. * this driver returns without entering firmware to avoid
  1176. * hanging up.
  1177. */
  1178. if (!spin_trylock_irqsave(&efivars->lock, flags))
  1179. return -EBUSY;
  1180. } else
  1181. spin_lock_irqsave(&efivars->lock, flags);
  1182. /*
  1183. * Check if there is a space enough to log.
  1184. * size: a size of logging data
  1185. * DUMP_NAME_LEN * 2: a maximum size of variable name
  1186. */
  1187. status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
  1188. size + DUMP_NAME_LEN * 2);
  1189. if (status) {
  1190. spin_unlock_irqrestore(&efivars->lock, flags);
  1191. *id = part;
  1192. return -ENOSPC;
  1193. }
  1194. sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
  1195. get_seconds());
  1196. for (i = 0; i < DUMP_NAME_LEN; i++)
  1197. efi_name[i] = name[i];
  1198. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  1199. size, psi->buf);
  1200. spin_unlock_irqrestore(&efivars->lock, flags);
  1201. if (reason == KMSG_DUMP_OOPS)
  1202. schedule_work(&efivar_work);
  1203. *id = part;
  1204. return ret;
  1205. };
  1206. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  1207. struct timespec time, struct pstore_info *psi)
  1208. {
  1209. char name[DUMP_NAME_LEN];
  1210. efi_char16_t efi_name[DUMP_NAME_LEN];
  1211. char name_old[DUMP_NAME_LEN];
  1212. efi_char16_t efi_name_old[DUMP_NAME_LEN];
  1213. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1214. struct efivars *efivars = psi->data;
  1215. struct efivar_entry *entry, *found = NULL;
  1216. int i;
  1217. sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
  1218. time.tv_sec);
  1219. spin_lock_irq(&efivars->lock);
  1220. for (i = 0; i < DUMP_NAME_LEN; i++)
  1221. efi_name[i] = name[i];
  1222. /*
  1223. * Clean up an entry with the same name
  1224. */
  1225. list_for_each_entry(entry, &efivars->list, list) {
  1226. get_var_data_locked(efivars, &entry->var);
  1227. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  1228. continue;
  1229. if (utf16_strncmp(entry->var.VariableName, efi_name,
  1230. utf16_strlen(efi_name))) {
  1231. /*
  1232. * Check if an old format,
  1233. * which doesn't support holding
  1234. * multiple logs, remains.
  1235. */
  1236. sprintf(name_old, "dump-type%u-%u-%lu", type,
  1237. (unsigned int)id, time.tv_sec);
  1238. for (i = 0; i < DUMP_NAME_LEN; i++)
  1239. efi_name_old[i] = name_old[i];
  1240. if (utf16_strncmp(entry->var.VariableName, efi_name_old,
  1241. utf16_strlen(efi_name_old)))
  1242. continue;
  1243. }
  1244. /* found */
  1245. found = entry;
  1246. efivars->ops->set_variable(entry->var.VariableName,
  1247. &entry->var.VendorGuid,
  1248. PSTORE_EFI_ATTRIBUTES,
  1249. 0, NULL);
  1250. break;
  1251. }
  1252. if (found)
  1253. list_del(&found->list);
  1254. spin_unlock_irq(&efivars->lock);
  1255. if (found)
  1256. efivar_unregister(found);
  1257. return 0;
  1258. }
  1259. static struct pstore_info efi_pstore_info = {
  1260. .owner = THIS_MODULE,
  1261. .name = "efi",
  1262. .open = efi_pstore_open,
  1263. .close = efi_pstore_close,
  1264. .read = efi_pstore_read,
  1265. .write = efi_pstore_write,
  1266. .erase = efi_pstore_erase,
  1267. };
  1268. static void efivar_pstore_register(struct efivars *efivars)
  1269. {
  1270. efivars->efi_pstore_info = efi_pstore_info;
  1271. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1272. if (efivars->efi_pstore_info.buf) {
  1273. efivars->efi_pstore_info.bufsize = 1024;
  1274. efivars->efi_pstore_info.data = efivars;
  1275. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1276. pstore_register(&efivars->efi_pstore_info);
  1277. }
  1278. }
  1279. #else
  1280. static void efivar_pstore_register(struct efivars *efivars)
  1281. {
  1282. return;
  1283. }
  1284. #endif
  1285. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  1286. struct bin_attribute *bin_attr,
  1287. char *buf, loff_t pos, size_t count)
  1288. {
  1289. struct efi_variable *new_var = (struct efi_variable *)buf;
  1290. struct efivars *efivars = bin_attr->private;
  1291. struct efivar_entry *search_efivar, *n;
  1292. unsigned long strsize1, strsize2;
  1293. efi_status_t status = EFI_NOT_FOUND;
  1294. int found = 0;
  1295. if (!capable(CAP_SYS_ADMIN))
  1296. return -EACCES;
  1297. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  1298. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  1299. printk(KERN_ERR "efivars: Malformed variable content\n");
  1300. return -EINVAL;
  1301. }
  1302. spin_lock_irq(&efivars->lock);
  1303. /*
  1304. * Does this variable already exist?
  1305. */
  1306. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1307. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1308. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  1309. if (strsize1 == strsize2 &&
  1310. !memcmp(&(search_efivar->var.VariableName),
  1311. new_var->VariableName, strsize1) &&
  1312. !efi_guidcmp(search_efivar->var.VendorGuid,
  1313. new_var->VendorGuid)) {
  1314. found = 1;
  1315. break;
  1316. }
  1317. }
  1318. if (found) {
  1319. spin_unlock_irq(&efivars->lock);
  1320. return -EINVAL;
  1321. }
  1322. status = check_var_size_locked(efivars, new_var->Attributes,
  1323. new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
  1324. if (status && status != EFI_UNSUPPORTED) {
  1325. spin_unlock_irq(&efivars->lock);
  1326. return efi_status_to_err(status);
  1327. }
  1328. /* now *really* create the variable via EFI */
  1329. status = efivars->ops->set_variable(new_var->VariableName,
  1330. &new_var->VendorGuid,
  1331. new_var->Attributes,
  1332. new_var->DataSize,
  1333. new_var->Data);
  1334. if (status != EFI_SUCCESS) {
  1335. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1336. status);
  1337. spin_unlock_irq(&efivars->lock);
  1338. return -EIO;
  1339. }
  1340. spin_unlock_irq(&efivars->lock);
  1341. /* Create the entry in sysfs. Locking is not required here */
  1342. status = efivar_create_sysfs_entry(efivars,
  1343. utf16_strsize(new_var->VariableName,
  1344. 1024),
  1345. new_var->VariableName,
  1346. &new_var->VendorGuid);
  1347. if (status) {
  1348. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  1349. }
  1350. return count;
  1351. }
  1352. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  1353. struct bin_attribute *bin_attr,
  1354. char *buf, loff_t pos, size_t count)
  1355. {
  1356. struct efi_variable *del_var = (struct efi_variable *)buf;
  1357. struct efivars *efivars = bin_attr->private;
  1358. struct efivar_entry *search_efivar, *n;
  1359. unsigned long strsize1, strsize2;
  1360. efi_status_t status = EFI_NOT_FOUND;
  1361. int found = 0;
  1362. if (!capable(CAP_SYS_ADMIN))
  1363. return -EACCES;
  1364. spin_lock_irq(&efivars->lock);
  1365. /*
  1366. * Does this variable already exist?
  1367. */
  1368. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1369. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1370. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  1371. if (strsize1 == strsize2 &&
  1372. !memcmp(&(search_efivar->var.VariableName),
  1373. del_var->VariableName, strsize1) &&
  1374. !efi_guidcmp(search_efivar->var.VendorGuid,
  1375. del_var->VendorGuid)) {
  1376. found = 1;
  1377. break;
  1378. }
  1379. }
  1380. if (!found) {
  1381. spin_unlock_irq(&efivars->lock);
  1382. return -EINVAL;
  1383. }
  1384. /* force the Attributes/DataSize to 0 to ensure deletion */
  1385. del_var->Attributes = 0;
  1386. del_var->DataSize = 0;
  1387. status = efivars->ops->set_variable(del_var->VariableName,
  1388. &del_var->VendorGuid,
  1389. del_var->Attributes,
  1390. del_var->DataSize,
  1391. del_var->Data);
  1392. if (status != EFI_SUCCESS) {
  1393. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1394. status);
  1395. spin_unlock_irq(&efivars->lock);
  1396. return -EIO;
  1397. }
  1398. list_del(&search_efivar->list);
  1399. /* We need to release this lock before unregistering. */
  1400. spin_unlock_irq(&efivars->lock);
  1401. efivar_unregister(search_efivar);
  1402. /* It's dead Jim.... */
  1403. return count;
  1404. }
  1405. static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
  1406. {
  1407. struct efivar_entry *entry, *n;
  1408. struct efivars *efivars = &__efivars;
  1409. unsigned long strsize1, strsize2;
  1410. bool found = false;
  1411. strsize1 = utf16_strsize(variable_name, 1024);
  1412. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1413. strsize2 = utf16_strsize(entry->var.VariableName, 1024);
  1414. if (strsize1 == strsize2 &&
  1415. !memcmp(variable_name, &(entry->var.VariableName),
  1416. strsize2) &&
  1417. !efi_guidcmp(entry->var.VendorGuid,
  1418. *vendor)) {
  1419. found = true;
  1420. break;
  1421. }
  1422. }
  1423. return found;
  1424. }
  1425. /*
  1426. * Returns the size of variable_name, in bytes, including the
  1427. * terminating NULL character, or variable_name_size if no NULL
  1428. * character is found among the first variable_name_size bytes.
  1429. */
  1430. static unsigned long var_name_strnsize(efi_char16_t *variable_name,
  1431. unsigned long variable_name_size)
  1432. {
  1433. unsigned long len;
  1434. efi_char16_t c;
  1435. /*
  1436. * The variable name is, by definition, a NULL-terminated
  1437. * string, so make absolutely sure that variable_name_size is
  1438. * the value we expect it to be. If not, return the real size.
  1439. */
  1440. for (len = 2; len <= variable_name_size; len += sizeof(c)) {
  1441. c = variable_name[(len / sizeof(c)) - 1];
  1442. if (!c)
  1443. break;
  1444. }
  1445. return min(len, variable_name_size);
  1446. }
  1447. static void efivar_update_sysfs_entries(struct work_struct *work)
  1448. {
  1449. struct efivars *efivars = &__efivars;
  1450. efi_guid_t vendor;
  1451. efi_char16_t *variable_name;
  1452. unsigned long variable_name_size = 1024;
  1453. efi_status_t status = EFI_NOT_FOUND;
  1454. bool found;
  1455. /* Add new sysfs entries */
  1456. while (1) {
  1457. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1458. if (!variable_name) {
  1459. pr_err("efivars: Memory allocation failed.\n");
  1460. return;
  1461. }
  1462. spin_lock_irq(&efivars->lock);
  1463. found = false;
  1464. while (1) {
  1465. variable_name_size = 1024;
  1466. status = efivars->ops->get_next_variable(
  1467. &variable_name_size,
  1468. variable_name,
  1469. &vendor);
  1470. if (status != EFI_SUCCESS) {
  1471. break;
  1472. } else {
  1473. if (!variable_is_present(variable_name,
  1474. &vendor)) {
  1475. found = true;
  1476. break;
  1477. }
  1478. }
  1479. }
  1480. spin_unlock_irq(&efivars->lock);
  1481. if (!found) {
  1482. kfree(variable_name);
  1483. break;
  1484. } else {
  1485. variable_name_size = var_name_strnsize(variable_name,
  1486. variable_name_size);
  1487. efivar_create_sysfs_entry(efivars,
  1488. variable_name_size,
  1489. variable_name, &vendor);
  1490. }
  1491. }
  1492. }
  1493. /*
  1494. * Let's not leave out systab information that snuck into
  1495. * the efivars driver
  1496. */
  1497. static ssize_t systab_show(struct kobject *kobj,
  1498. struct kobj_attribute *attr, char *buf)
  1499. {
  1500. char *str = buf;
  1501. if (!kobj || !buf)
  1502. return -EINVAL;
  1503. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1504. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1505. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1506. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1507. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1508. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1509. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1510. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1511. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1512. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1513. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1514. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1515. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1516. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1517. return str - buf;
  1518. }
  1519. static struct kobj_attribute efi_attr_systab =
  1520. __ATTR(systab, 0400, systab_show, NULL);
  1521. static struct attribute *efi_subsys_attrs[] = {
  1522. &efi_attr_systab.attr,
  1523. NULL, /* maybe more in the future? */
  1524. };
  1525. static struct attribute_group efi_subsys_attr_group = {
  1526. .attrs = efi_subsys_attrs,
  1527. };
  1528. static struct kobject *efi_kobj;
  1529. /*
  1530. * efivar_create_sysfs_entry()
  1531. * Requires:
  1532. * variable_name_size = number of bytes required to hold
  1533. * variable_name (not counting the NULL
  1534. * character at the end.
  1535. * efivars->lock is not held on entry or exit.
  1536. * Returns 1 on failure, 0 on success
  1537. */
  1538. static int
  1539. efivar_create_sysfs_entry(struct efivars *efivars,
  1540. unsigned long variable_name_size,
  1541. efi_char16_t *variable_name,
  1542. efi_guid_t *vendor_guid)
  1543. {
  1544. int i, short_name_size;
  1545. char *short_name;
  1546. struct efivar_entry *new_efivar;
  1547. /*
  1548. * Length of the variable bytes in ASCII, plus the '-' separator,
  1549. * plus the GUID, plus trailing NUL
  1550. */
  1551. short_name_size = variable_name_size / sizeof(efi_char16_t)
  1552. + 1 + GUID_LEN + 1;
  1553. short_name = kzalloc(short_name_size, GFP_KERNEL);
  1554. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  1555. if (!short_name || !new_efivar) {
  1556. kfree(short_name);
  1557. kfree(new_efivar);
  1558. return 1;
  1559. }
  1560. new_efivar->efivars = efivars;
  1561. memcpy(new_efivar->var.VariableName, variable_name,
  1562. variable_name_size);
  1563. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1564. /* Convert Unicode to normal chars (assume top bits are 0),
  1565. ala UTF-8 */
  1566. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1567. short_name[i] = variable_name[i] & 0xFF;
  1568. }
  1569. /* This is ugly, but necessary to separate one vendor's
  1570. private variables from another's. */
  1571. *(short_name + strlen(short_name)) = '-';
  1572. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1573. new_efivar->kobj.kset = efivars->kset;
  1574. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1575. "%s", short_name);
  1576. if (i) {
  1577. kfree(short_name);
  1578. kfree(new_efivar);
  1579. return 1;
  1580. }
  1581. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1582. kfree(short_name);
  1583. short_name = NULL;
  1584. spin_lock_irq(&efivars->lock);
  1585. list_add(&new_efivar->list, &efivars->list);
  1586. spin_unlock_irq(&efivars->lock);
  1587. return 0;
  1588. }
  1589. static int
  1590. create_efivars_bin_attributes(struct efivars *efivars)
  1591. {
  1592. struct bin_attribute *attr;
  1593. int error;
  1594. /* new_var */
  1595. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1596. if (!attr)
  1597. return -ENOMEM;
  1598. attr->attr.name = "new_var";
  1599. attr->attr.mode = 0200;
  1600. attr->write = efivar_create;
  1601. attr->private = efivars;
  1602. efivars->new_var = attr;
  1603. /* del_var */
  1604. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1605. if (!attr) {
  1606. error = -ENOMEM;
  1607. goto out_free;
  1608. }
  1609. attr->attr.name = "del_var";
  1610. attr->attr.mode = 0200;
  1611. attr->write = efivar_delete;
  1612. attr->private = efivars;
  1613. efivars->del_var = attr;
  1614. sysfs_bin_attr_init(efivars->new_var);
  1615. sysfs_bin_attr_init(efivars->del_var);
  1616. /* Register */
  1617. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1618. efivars->new_var);
  1619. if (error) {
  1620. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1621. " due to error %d\n", error);
  1622. goto out_free;
  1623. }
  1624. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1625. efivars->del_var);
  1626. if (error) {
  1627. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1628. " due to error %d\n", error);
  1629. sysfs_remove_bin_file(&efivars->kset->kobj,
  1630. efivars->new_var);
  1631. goto out_free;
  1632. }
  1633. return 0;
  1634. out_free:
  1635. kfree(efivars->del_var);
  1636. efivars->del_var = NULL;
  1637. kfree(efivars->new_var);
  1638. efivars->new_var = NULL;
  1639. return error;
  1640. }
  1641. void unregister_efivars(struct efivars *efivars)
  1642. {
  1643. struct efivar_entry *entry, *n;
  1644. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1645. spin_lock_irq(&efivars->lock);
  1646. list_del(&entry->list);
  1647. spin_unlock_irq(&efivars->lock);
  1648. efivar_unregister(entry);
  1649. }
  1650. if (efivars->new_var)
  1651. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1652. if (efivars->del_var)
  1653. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1654. kfree(efivars->new_var);
  1655. kfree(efivars->del_var);
  1656. kobject_put(efivars->kobject);
  1657. kset_unregister(efivars->kset);
  1658. }
  1659. EXPORT_SYMBOL_GPL(unregister_efivars);
  1660. int register_efivars(struct efivars *efivars,
  1661. const struct efivar_operations *ops,
  1662. struct kobject *parent_kobj)
  1663. {
  1664. efi_status_t status = EFI_NOT_FOUND;
  1665. efi_guid_t vendor_guid;
  1666. efi_char16_t *variable_name;
  1667. unsigned long variable_name_size = 1024;
  1668. int error = 0;
  1669. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1670. if (!variable_name) {
  1671. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1672. return -ENOMEM;
  1673. }
  1674. spin_lock_init(&efivars->lock);
  1675. INIT_LIST_HEAD(&efivars->list);
  1676. efivars->ops = ops;
  1677. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1678. if (!efivars->kset) {
  1679. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1680. error = -ENOMEM;
  1681. goto out;
  1682. }
  1683. efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
  1684. if (!efivars->kobject) {
  1685. pr_err("efivars: Subsystem registration failed.\n");
  1686. error = -ENOMEM;
  1687. kset_unregister(efivars->kset);
  1688. goto out;
  1689. }
  1690. /*
  1691. * Per EFI spec, the maximum storage allocated for both
  1692. * the variable name and variable data is 1024 bytes.
  1693. */
  1694. do {
  1695. variable_name_size = 1024;
  1696. status = ops->get_next_variable(&variable_name_size,
  1697. variable_name,
  1698. &vendor_guid);
  1699. switch (status) {
  1700. case EFI_SUCCESS:
  1701. variable_name_size = var_name_strnsize(variable_name,
  1702. variable_name_size);
  1703. efivar_create_sysfs_entry(efivars,
  1704. variable_name_size,
  1705. variable_name,
  1706. &vendor_guid);
  1707. break;
  1708. case EFI_NOT_FOUND:
  1709. break;
  1710. default:
  1711. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1712. status);
  1713. status = EFI_NOT_FOUND;
  1714. break;
  1715. }
  1716. } while (status != EFI_NOT_FOUND);
  1717. error = create_efivars_bin_attributes(efivars);
  1718. if (error)
  1719. unregister_efivars(efivars);
  1720. if (!efivars_pstore_disable)
  1721. efivar_pstore_register(efivars);
  1722. register_filesystem(&efivarfs_type);
  1723. out:
  1724. kfree(variable_name);
  1725. return error;
  1726. }
  1727. EXPORT_SYMBOL_GPL(register_efivars);
  1728. /*
  1729. * For now we register the efi subsystem with the firmware subsystem
  1730. * and the vars subsystem with the efi subsystem. In the future, it
  1731. * might make sense to split off the efi subsystem into its own
  1732. * driver, but for now only efivars will register with it, so just
  1733. * include it here.
  1734. */
  1735. static int __init
  1736. efivars_init(void)
  1737. {
  1738. int error = 0;
  1739. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1740. EFIVARS_DATE);
  1741. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  1742. return 0;
  1743. /* For now we'll register the efi directory at /sys/firmware/efi */
  1744. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1745. if (!efi_kobj) {
  1746. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1747. return -ENOMEM;
  1748. }
  1749. ops.get_variable = efi.get_variable;
  1750. ops.set_variable = efi.set_variable;
  1751. ops.get_next_variable = efi.get_next_variable;
  1752. ops.query_variable_info = efi.query_variable_info;
  1753. error = register_efivars(&__efivars, &ops, efi_kobj);
  1754. if (error)
  1755. goto err_put;
  1756. /* Don't forget the systab entry */
  1757. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1758. if (error) {
  1759. printk(KERN_ERR
  1760. "efivars: Sysfs attribute export failed with error %d.\n",
  1761. error);
  1762. goto err_unregister;
  1763. }
  1764. return 0;
  1765. err_unregister:
  1766. unregister_efivars(&__efivars);
  1767. err_put:
  1768. kobject_put(efi_kobj);
  1769. return error;
  1770. }
  1771. static void __exit
  1772. efivars_exit(void)
  1773. {
  1774. cancel_work_sync(&efivar_work);
  1775. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  1776. unregister_efivars(&__efivars);
  1777. kobject_put(efi_kobj);
  1778. }
  1779. }
  1780. module_init(efivars_init);
  1781. module_exit(efivars_exit);