efivars.c 52 KB

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