efivars.c 50 KB

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