efivars.c 54 KB

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