efivars.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. /*
  2. * EFI Variables - efivars.c
  3. *
  4. * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
  5. * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
  6. *
  7. * This code takes all variables accessible from EFI runtime and
  8. * exports them via sysfs
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Changelog:
  25. *
  26. * 17 May 2004 - Matt Domsch <Matt_Domsch@dell.com>
  27. * remove check for efi_enabled in exit
  28. * add MODULE_VERSION
  29. *
  30. * 26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com>
  31. * minor bug fixes
  32. *
  33. * 21 Apr 2004 - Matt Tolentino <matthew.e.tolentino@intel.com)
  34. * converted driver to export variable information via sysfs
  35. * and moved to drivers/firmware directory
  36. * bumped revision number to v0.07 to reflect conversion & move
  37. *
  38. * 10 Dec 2002 - Matt Domsch <Matt_Domsch@dell.com>
  39. * fix locking per Peter Chubb's findings
  40. *
  41. * 25 Mar 2002 - Matt Domsch <Matt_Domsch@dell.com>
  42. * move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_unparse()
  43. *
  44. * 12 Feb 2002 - Matt Domsch <Matt_Domsch@dell.com>
  45. * use list_for_each_safe when deleting vars.
  46. * remove ifdef CONFIG_SMP around include <linux/smp.h>
  47. * v0.04 release to linux-ia64@linuxia64.org
  48. *
  49. * 20 April 2001 - Matt Domsch <Matt_Domsch@dell.com>
  50. * Moved vars from /proc/efi to /proc/efi/vars, and made
  51. * efi.c own the /proc/efi directory.
  52. * v0.03 release to linux-ia64@linuxia64.org
  53. *
  54. * 26 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
  55. * At the request of Stephane, moved ownership of /proc/efi
  56. * to efi.c, and now efivars lives under /proc/efi/vars.
  57. *
  58. * 12 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
  59. * Feedback received from Stephane Eranian incorporated.
  60. * efivar_write() checks copy_from_user() return value.
  61. * efivar_read/write() returns proper errno.
  62. * v0.02 release to linux-ia64@linuxia64.org
  63. *
  64. * 26 February 2001 - Matt Domsch <Matt_Domsch@dell.com>
  65. * v0.01 release to linux-ia64@linuxia64.org
  66. */
  67. #include <linux/capability.h>
  68. #include <linux/types.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/mm.h>
  72. #include <linux/module.h>
  73. #include <linux/string.h>
  74. #include <linux/smp.h>
  75. #include <linux/efi.h>
  76. #include <linux/sysfs.h>
  77. #include <linux/kobject.h>
  78. #include <linux/device.h>
  79. #include <linux/slab.h>
  80. #include <linux/pstore.h>
  81. #include <linux/fs.h>
  82. #include <linux/ramfs.h>
  83. #include <linux/pagemap.h>
  84. #include <asm/uaccess.h>
  85. #define EFIVARS_VERSION "0.08"
  86. #define EFIVARS_DATE "2004-May-17"
  87. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  88. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  89. MODULE_LICENSE("GPL");
  90. MODULE_VERSION(EFIVARS_VERSION);
  91. #define DUMP_NAME_LEN 52
  92. #define GUID_LEN 37
  93. /*
  94. * The maximum size of VariableName + Data = 1024
  95. * Therefore, it's reasonable to save that much
  96. * space in each part of the structure,
  97. * and we use a page for reading/writing.
  98. */
  99. struct efi_variable {
  100. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  101. efi_guid_t VendorGuid;
  102. unsigned long DataSize;
  103. __u8 Data[1024];
  104. efi_status_t Status;
  105. __u32 Attributes;
  106. } __attribute__((packed));
  107. struct efivar_entry {
  108. struct efivars *efivars;
  109. struct efi_variable var;
  110. struct list_head list;
  111. struct kobject kobj;
  112. };
  113. struct efivar_attribute {
  114. struct attribute attr;
  115. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  116. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  117. };
  118. static struct efivars __efivars;
  119. static struct efivar_operations ops;
  120. #define PSTORE_EFI_ATTRIBUTES \
  121. (EFI_VARIABLE_NON_VOLATILE | \
  122. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  123. EFI_VARIABLE_RUNTIME_ACCESS)
  124. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  125. struct efivar_attribute efivar_attr_##_name = { \
  126. .attr = {.name = __stringify(_name), .mode = _mode}, \
  127. .show = _show, \
  128. .store = _store, \
  129. };
  130. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  131. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  132. /*
  133. * Prototype for sysfs creation function
  134. */
  135. static int
  136. efivar_create_sysfs_entry(struct efivars *efivars,
  137. unsigned long variable_name_size,
  138. efi_char16_t *variable_name,
  139. efi_guid_t *vendor_guid);
  140. /* Return the number of unicode characters in data */
  141. static unsigned long
  142. utf16_strnlen(efi_char16_t *s, size_t maxlength)
  143. {
  144. unsigned long length = 0;
  145. while (*s++ != 0 && length < maxlength)
  146. length++;
  147. return length;
  148. }
  149. static inline unsigned long
  150. utf16_strlen(efi_char16_t *s)
  151. {
  152. return utf16_strnlen(s, ~0UL);
  153. }
  154. /*
  155. * Return the number of bytes is the length of this string
  156. * Note: this is NOT the same as the number of unicode characters
  157. */
  158. static inline unsigned long
  159. utf16_strsize(efi_char16_t *data, unsigned long maxlength)
  160. {
  161. return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
  162. }
  163. static inline int
  164. utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
  165. {
  166. while (1) {
  167. if (len == 0)
  168. return 0;
  169. if (*a < *b)
  170. return -1;
  171. if (*a > *b)
  172. return 1;
  173. if (*a == 0) /* implies *b == 0 */
  174. return 0;
  175. a++;
  176. b++;
  177. len--;
  178. }
  179. }
  180. static bool
  181. validate_device_path(struct efi_variable *var, int match, u8 *buffer,
  182. unsigned long len)
  183. {
  184. struct efi_generic_dev_path *node;
  185. int offset = 0;
  186. node = (struct efi_generic_dev_path *)buffer;
  187. if (len < sizeof(*node))
  188. return false;
  189. while (offset <= len - sizeof(*node) &&
  190. node->length >= sizeof(*node) &&
  191. node->length <= len - offset) {
  192. offset += node->length;
  193. if ((node->type == EFI_DEV_END_PATH ||
  194. node->type == EFI_DEV_END_PATH2) &&
  195. node->sub_type == EFI_DEV_END_ENTIRE)
  196. return true;
  197. node = (struct efi_generic_dev_path *)(buffer + offset);
  198. }
  199. /*
  200. * If we're here then either node->length pointed past the end
  201. * of the buffer or we reached the end of the buffer without
  202. * finding a device path end node.
  203. */
  204. return false;
  205. }
  206. static bool
  207. validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
  208. unsigned long len)
  209. {
  210. /* An array of 16-bit integers */
  211. if ((len % 2) != 0)
  212. return false;
  213. return true;
  214. }
  215. static bool
  216. validate_load_option(struct efi_variable *var, int match, u8 *buffer,
  217. unsigned long len)
  218. {
  219. u16 filepathlength;
  220. int i, desclength = 0, namelen;
  221. namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
  222. /* Either "Boot" or "Driver" followed by four digits of hex */
  223. for (i = match; i < match+4; i++) {
  224. if (var->VariableName[i] > 127 ||
  225. hex_to_bin(var->VariableName[i] & 0xff) < 0)
  226. return true;
  227. }
  228. /* Reject it if there's 4 digits of hex and then further content */
  229. if (namelen > match + 4)
  230. return false;
  231. /* A valid entry must be at least 8 bytes */
  232. if (len < 8)
  233. return false;
  234. filepathlength = buffer[4] | buffer[5] << 8;
  235. /*
  236. * There's no stored length for the description, so it has to be
  237. * found by hand
  238. */
  239. desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  240. /* Each boot entry must have a descriptor */
  241. if (!desclength)
  242. return false;
  243. /*
  244. * If the sum of the length of the description, the claimed filepath
  245. * length and the original header are greater than the length of the
  246. * variable, it's malformed
  247. */
  248. if ((desclength + filepathlength + 6) > len)
  249. return false;
  250. /*
  251. * And, finally, check the filepath
  252. */
  253. return validate_device_path(var, match, buffer + desclength + 6,
  254. filepathlength);
  255. }
  256. static bool
  257. validate_uint16(struct efi_variable *var, int match, u8 *buffer,
  258. unsigned long len)
  259. {
  260. /* A single 16-bit integer */
  261. if (len != 2)
  262. return false;
  263. return true;
  264. }
  265. static bool
  266. validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
  267. unsigned long len)
  268. {
  269. int i;
  270. for (i = 0; i < len; i++) {
  271. if (buffer[i] > 127)
  272. return false;
  273. if (buffer[i] == 0)
  274. return true;
  275. }
  276. return false;
  277. }
  278. struct variable_validate {
  279. char *name;
  280. bool (*validate)(struct efi_variable *var, int match, u8 *data,
  281. unsigned long len);
  282. };
  283. static const struct variable_validate variable_validate[] = {
  284. { "BootNext", validate_uint16 },
  285. { "BootOrder", validate_boot_order },
  286. { "DriverOrder", validate_boot_order },
  287. { "Boot*", validate_load_option },
  288. { "Driver*", validate_load_option },
  289. { "ConIn", validate_device_path },
  290. { "ConInDev", validate_device_path },
  291. { "ConOut", validate_device_path },
  292. { "ConOutDev", validate_device_path },
  293. { "ErrOut", validate_device_path },
  294. { "ErrOutDev", validate_device_path },
  295. { "Timeout", validate_uint16 },
  296. { "Lang", validate_ascii_string },
  297. { "PlatformLang", validate_ascii_string },
  298. { "", NULL },
  299. };
  300. static bool
  301. validate_var(struct efi_variable *var, u8 *data, unsigned long len)
  302. {
  303. int i;
  304. u16 *unicode_name = var->VariableName;
  305. for (i = 0; variable_validate[i].validate != NULL; i++) {
  306. const char *name = variable_validate[i].name;
  307. int match;
  308. for (match = 0; ; match++) {
  309. char c = name[match];
  310. u16 u = unicode_name[match];
  311. /* All special variables are plain ascii */
  312. if (u > 127)
  313. return true;
  314. /* Wildcard in the matching name means we've matched */
  315. if (c == '*')
  316. return variable_validate[i].validate(var,
  317. match, data, len);
  318. /* Case sensitive match */
  319. if (c != u)
  320. break;
  321. /* Reached the end of the string while matching */
  322. if (!c)
  323. return variable_validate[i].validate(var,
  324. match, data, len);
  325. }
  326. }
  327. return true;
  328. }
  329. static efi_status_t
  330. get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
  331. {
  332. efi_status_t status;
  333. var->DataSize = 1024;
  334. status = efivars->ops->get_variable(var->VariableName,
  335. &var->VendorGuid,
  336. &var->Attributes,
  337. &var->DataSize,
  338. var->Data);
  339. return status;
  340. }
  341. static efi_status_t
  342. get_var_data(struct efivars *efivars, struct efi_variable *var)
  343. {
  344. efi_status_t status;
  345. spin_lock(&efivars->lock);
  346. status = get_var_data_locked(efivars, var);
  347. spin_unlock(&efivars->lock);
  348. if (status != EFI_SUCCESS) {
  349. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  350. status);
  351. }
  352. return status;
  353. }
  354. static ssize_t
  355. efivar_guid_read(struct efivar_entry *entry, char *buf)
  356. {
  357. struct efi_variable *var = &entry->var;
  358. char *str = buf;
  359. if (!entry || !buf)
  360. return 0;
  361. efi_guid_unparse(&var->VendorGuid, str);
  362. str += strlen(str);
  363. str += sprintf(str, "\n");
  364. return str - buf;
  365. }
  366. static ssize_t
  367. efivar_attr_read(struct efivar_entry *entry, char *buf)
  368. {
  369. struct efi_variable *var = &entry->var;
  370. char *str = buf;
  371. efi_status_t status;
  372. if (!entry || !buf)
  373. return -EINVAL;
  374. status = get_var_data(entry->efivars, var);
  375. if (status != EFI_SUCCESS)
  376. return -EIO;
  377. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  378. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  379. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  380. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  381. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  382. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  383. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  384. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  385. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  386. str += sprintf(str,
  387. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  388. if (var->Attributes &
  389. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  390. str += sprintf(str,
  391. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  392. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  393. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  394. return str - buf;
  395. }
  396. static ssize_t
  397. efivar_size_read(struct efivar_entry *entry, char *buf)
  398. {
  399. struct efi_variable *var = &entry->var;
  400. char *str = buf;
  401. efi_status_t status;
  402. if (!entry || !buf)
  403. return -EINVAL;
  404. status = get_var_data(entry->efivars, var);
  405. if (status != EFI_SUCCESS)
  406. return -EIO;
  407. str += sprintf(str, "0x%lx\n", var->DataSize);
  408. return str - buf;
  409. }
  410. static ssize_t
  411. efivar_data_read(struct efivar_entry *entry, char *buf)
  412. {
  413. struct efi_variable *var = &entry->var;
  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. memcpy(buf, var->Data, var->DataSize);
  421. return var->DataSize;
  422. }
  423. /*
  424. * We allow each variable to be edited via rewriting the
  425. * entire efi variable structure.
  426. */
  427. static ssize_t
  428. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  429. {
  430. struct efi_variable *new_var, *var = &entry->var;
  431. struct efivars *efivars = entry->efivars;
  432. efi_status_t status = EFI_NOT_FOUND;
  433. if (count != sizeof(struct efi_variable))
  434. return -EINVAL;
  435. new_var = (struct efi_variable *)buf;
  436. /*
  437. * If only updating the variable data, then the name
  438. * and guid should remain the same
  439. */
  440. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  441. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  442. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  443. return -EINVAL;
  444. }
  445. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  446. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  447. return -EINVAL;
  448. }
  449. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  450. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  451. printk(KERN_ERR "efivars: Malformed variable content\n");
  452. return -EINVAL;
  453. }
  454. spin_lock(&efivars->lock);
  455. status = efivars->ops->set_variable(new_var->VariableName,
  456. &new_var->VendorGuid,
  457. new_var->Attributes,
  458. new_var->DataSize,
  459. new_var->Data);
  460. spin_unlock(&efivars->lock);
  461. if (status != EFI_SUCCESS) {
  462. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  463. status);
  464. return -EIO;
  465. }
  466. memcpy(&entry->var, new_var, count);
  467. return count;
  468. }
  469. static ssize_t
  470. efivar_show_raw(struct efivar_entry *entry, char *buf)
  471. {
  472. struct efi_variable *var = &entry->var;
  473. efi_status_t status;
  474. if (!entry || !buf)
  475. return 0;
  476. status = get_var_data(entry->efivars, var);
  477. if (status != EFI_SUCCESS)
  478. return -EIO;
  479. memcpy(buf, var, sizeof(*var));
  480. return sizeof(*var);
  481. }
  482. /*
  483. * Generic read/write functions that call the specific functions of
  484. * the attributes...
  485. */
  486. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  487. char *buf)
  488. {
  489. struct efivar_entry *var = to_efivar_entry(kobj);
  490. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  491. ssize_t ret = -EIO;
  492. if (!capable(CAP_SYS_ADMIN))
  493. return -EACCES;
  494. if (efivar_attr->show) {
  495. ret = efivar_attr->show(var, buf);
  496. }
  497. return ret;
  498. }
  499. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  500. const char *buf, size_t count)
  501. {
  502. struct efivar_entry *var = to_efivar_entry(kobj);
  503. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  504. ssize_t ret = -EIO;
  505. if (!capable(CAP_SYS_ADMIN))
  506. return -EACCES;
  507. if (efivar_attr->store)
  508. ret = efivar_attr->store(var, buf, count);
  509. return ret;
  510. }
  511. static const struct sysfs_ops efivar_attr_ops = {
  512. .show = efivar_attr_show,
  513. .store = efivar_attr_store,
  514. };
  515. static void efivar_release(struct kobject *kobj)
  516. {
  517. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  518. kfree(var);
  519. }
  520. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  521. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  522. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  523. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  524. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  525. static struct attribute *def_attrs[] = {
  526. &efivar_attr_guid.attr,
  527. &efivar_attr_size.attr,
  528. &efivar_attr_attributes.attr,
  529. &efivar_attr_data.attr,
  530. &efivar_attr_raw_var.attr,
  531. NULL,
  532. };
  533. static struct kobj_type efivar_ktype = {
  534. .release = efivar_release,
  535. .sysfs_ops = &efivar_attr_ops,
  536. .default_attrs = def_attrs,
  537. };
  538. static inline void
  539. efivar_unregister(struct efivar_entry *var)
  540. {
  541. kobject_put(&var->kobj);
  542. }
  543. static int efivarfs_file_open(struct inode *inode, struct file *file)
  544. {
  545. file->private_data = inode->i_private;
  546. return 0;
  547. }
  548. static ssize_t efivarfs_file_write(struct file *file,
  549. const char __user *userbuf, size_t count, loff_t *ppos)
  550. {
  551. struct efivar_entry *var = file->private_data;
  552. struct efivars *efivars;
  553. efi_status_t status;
  554. void *data;
  555. u32 attributes;
  556. struct inode *inode = file->f_mapping->host;
  557. int datasize = count - sizeof(attributes);
  558. unsigned long newdatasize;
  559. if (count < sizeof(attributes))
  560. return -EINVAL;
  561. data = kmalloc(datasize, GFP_KERNEL);
  562. if (!data)
  563. return -ENOMEM;
  564. efivars = var->efivars;
  565. if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
  566. count = -EFAULT;
  567. goto out;
  568. }
  569. if (attributes & ~(EFI_VARIABLE_MASK)) {
  570. count = -EINVAL;
  571. goto out;
  572. }
  573. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  574. count = -EFAULT;
  575. goto out;
  576. }
  577. if (validate_var(&var->var, data, datasize) == false) {
  578. count = -EINVAL;
  579. goto out;
  580. }
  581. status = efivars->ops->set_variable(var->var.VariableName,
  582. &var->var.VendorGuid,
  583. attributes, datasize,
  584. data);
  585. switch (status) {
  586. case EFI_SUCCESS:
  587. break;
  588. case EFI_INVALID_PARAMETER:
  589. count = -EINVAL;
  590. goto out;
  591. case EFI_OUT_OF_RESOURCES:
  592. count = -ENOSPC;
  593. goto out;
  594. case EFI_DEVICE_ERROR:
  595. count = -EIO;
  596. goto out;
  597. case EFI_WRITE_PROTECTED:
  598. count = -EROFS;
  599. goto out;
  600. case EFI_SECURITY_VIOLATION:
  601. count = -EACCES;
  602. goto out;
  603. case EFI_NOT_FOUND:
  604. count = -ENOENT;
  605. goto out;
  606. default:
  607. count = -EINVAL;
  608. goto out;
  609. }
  610. /*
  611. * Writing to the variable may have caused a change in size (which
  612. * could either be an append or an overwrite), or the variable to be
  613. * deleted. Perform a GetVariable() so we can tell what actually
  614. * happened.
  615. */
  616. newdatasize = 0;
  617. status = efivars->ops->get_variable(var->var.VariableName,
  618. &var->var.VendorGuid,
  619. NULL, &newdatasize,
  620. NULL);
  621. if (status == EFI_BUFFER_TOO_SMALL) {
  622. mutex_lock(&inode->i_mutex);
  623. i_size_write(inode, newdatasize + sizeof(attributes));
  624. mutex_unlock(&inode->i_mutex);
  625. } else if (status == EFI_NOT_FOUND) {
  626. spin_lock(&efivars->lock);
  627. list_del(&var->list);
  628. spin_unlock(&efivars->lock);
  629. efivar_unregister(var);
  630. drop_nlink(inode);
  631. dput(file->f_dentry);
  632. } else {
  633. pr_warn("efivarfs: inconsistent EFI variable implementation? "
  634. "status = %lx\n", status);
  635. }
  636. out:
  637. kfree(data);
  638. return count;
  639. }
  640. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  641. size_t count, loff_t *ppos)
  642. {
  643. struct efivar_entry *var = file->private_data;
  644. struct efivars *efivars = var->efivars;
  645. efi_status_t status;
  646. unsigned long datasize = 0;
  647. u32 attributes;
  648. void *data;
  649. ssize_t size = 0;
  650. status = efivars->ops->get_variable(var->var.VariableName,
  651. &var->var.VendorGuid,
  652. &attributes, &datasize, NULL);
  653. if (status != EFI_BUFFER_TOO_SMALL)
  654. return 0;
  655. data = kmalloc(datasize + 4, GFP_KERNEL);
  656. if (!data)
  657. return 0;
  658. status = efivars->ops->get_variable(var->var.VariableName,
  659. &var->var.VendorGuid,
  660. &attributes, &datasize,
  661. (data + 4));
  662. if (status != EFI_SUCCESS)
  663. goto out_free;
  664. memcpy(data, &attributes, 4);
  665. size = simple_read_from_buffer(userbuf, count, ppos,
  666. data, datasize + 4);
  667. out_free:
  668. kfree(data);
  669. return size;
  670. }
  671. static void efivarfs_evict_inode(struct inode *inode)
  672. {
  673. clear_inode(inode);
  674. }
  675. static const struct super_operations efivarfs_ops = {
  676. .statfs = simple_statfs,
  677. .drop_inode = generic_delete_inode,
  678. .evict_inode = efivarfs_evict_inode,
  679. .show_options = generic_show_options,
  680. };
  681. static struct super_block *efivarfs_sb;
  682. static const struct inode_operations efivarfs_dir_inode_operations;
  683. static const struct file_operations efivarfs_file_operations = {
  684. .open = efivarfs_file_open,
  685. .read = efivarfs_file_read,
  686. .write = efivarfs_file_write,
  687. .llseek = no_llseek,
  688. };
  689. static struct inode *efivarfs_get_inode(struct super_block *sb,
  690. const struct inode *dir, int mode, dev_t dev)
  691. {
  692. struct inode *inode = new_inode(sb);
  693. if (inode) {
  694. inode->i_ino = get_next_ino();
  695. inode->i_uid = inode->i_gid = 0;
  696. inode->i_mode = mode;
  697. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  698. switch (mode & S_IFMT) {
  699. case S_IFREG:
  700. inode->i_fop = &efivarfs_file_operations;
  701. break;
  702. case S_IFDIR:
  703. inode->i_op = &efivarfs_dir_inode_operations;
  704. inode->i_fop = &simple_dir_operations;
  705. inc_nlink(inode);
  706. break;
  707. }
  708. }
  709. return inode;
  710. }
  711. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  712. {
  713. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  714. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  715. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  716. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  717. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  718. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  719. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  720. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  721. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  722. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  723. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  724. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  725. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  726. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  727. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  728. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  729. }
  730. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  731. umode_t mode, bool excl)
  732. {
  733. struct inode *inode;
  734. struct efivars *efivars = &__efivars;
  735. struct efivar_entry *var;
  736. int namelen, i = 0, err = 0;
  737. if (dentry->d_name.len < 38)
  738. return -EINVAL;
  739. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  740. if (!inode)
  741. return -ENOSPC;
  742. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  743. if (!var) {
  744. err = -ENOMEM;
  745. goto out;
  746. }
  747. namelen = dentry->d_name.len - GUID_LEN;
  748. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  749. &var->var.VendorGuid);
  750. for (i = 0; i < namelen; i++)
  751. var->var.VariableName[i] = dentry->d_name.name[i];
  752. var->var.VariableName[i] = '\0';
  753. inode->i_private = var;
  754. var->efivars = efivars;
  755. var->kobj.kset = efivars->kset;
  756. err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
  757. dentry->d_name.name);
  758. if (err)
  759. goto out;
  760. kobject_uevent(&var->kobj, KOBJ_ADD);
  761. spin_lock(&efivars->lock);
  762. list_add(&var->list, &efivars->list);
  763. spin_unlock(&efivars->lock);
  764. d_instantiate(dentry, inode);
  765. dget(dentry);
  766. out:
  767. if (err) {
  768. kfree(var);
  769. iput(inode);
  770. }
  771. return err;
  772. }
  773. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  774. {
  775. struct efivar_entry *var = dentry->d_inode->i_private;
  776. struct efivars *efivars = var->efivars;
  777. efi_status_t status;
  778. spin_lock(&efivars->lock);
  779. status = efivars->ops->set_variable(var->var.VariableName,
  780. &var->var.VendorGuid,
  781. 0, 0, NULL);
  782. if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
  783. list_del(&var->list);
  784. spin_unlock(&efivars->lock);
  785. efivar_unregister(var);
  786. drop_nlink(dir);
  787. dput(dentry);
  788. return 0;
  789. }
  790. spin_unlock(&efivars->lock);
  791. return -EINVAL;
  792. };
  793. int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  794. {
  795. struct inode *inode = NULL;
  796. struct dentry *root;
  797. struct efivar_entry *entry, *n;
  798. struct efivars *efivars = &__efivars;
  799. efivarfs_sb = sb;
  800. sb->s_maxbytes = MAX_LFS_FILESIZE;
  801. sb->s_blocksize = PAGE_CACHE_SIZE;
  802. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  803. sb->s_magic = PSTOREFS_MAGIC;
  804. sb->s_op = &efivarfs_ops;
  805. sb->s_time_gran = 1;
  806. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  807. if (!inode)
  808. return -ENOMEM;
  809. inode->i_op = &efivarfs_dir_inode_operations;
  810. root = d_make_root(inode);
  811. sb->s_root = root;
  812. if (!root)
  813. return -ENOMEM;
  814. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  815. struct inode *inode;
  816. struct dentry *dentry, *root = efivarfs_sb->s_root;
  817. char *name;
  818. unsigned long size = 0;
  819. int len, i;
  820. len = utf16_strlen(entry->var.VariableName);
  821. /* GUID plus trailing NULL */
  822. name = kmalloc(len + 38, GFP_ATOMIC);
  823. for (i = 0; i < len; i++)
  824. name[i] = entry->var.VariableName[i] & 0xFF;
  825. name[len] = '-';
  826. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  827. name[len+GUID_LEN] = '\0';
  828. inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
  829. S_IFREG | 0644, 0);
  830. dentry = d_alloc_name(root, name);
  831. efivars->ops->get_variable(entry->var.VariableName,
  832. &entry->var.VendorGuid,
  833. &entry->var.Attributes,
  834. &size,
  835. NULL);
  836. mutex_lock(&inode->i_mutex);
  837. inode->i_private = entry;
  838. i_size_write(inode, size+4);
  839. mutex_unlock(&inode->i_mutex);
  840. d_add(dentry, inode);
  841. }
  842. return 0;
  843. }
  844. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  845. int flags, const char *dev_name, void *data)
  846. {
  847. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  848. }
  849. static void efivarfs_kill_sb(struct super_block *sb)
  850. {
  851. kill_litter_super(sb);
  852. efivarfs_sb = NULL;
  853. }
  854. static struct file_system_type efivarfs_type = {
  855. .name = "efivarfs",
  856. .mount = efivarfs_mount,
  857. .kill_sb = efivarfs_kill_sb,
  858. };
  859. static const struct inode_operations efivarfs_dir_inode_operations = {
  860. .lookup = simple_lookup,
  861. .unlink = efivarfs_unlink,
  862. .create = efivarfs_create,
  863. };
  864. static struct pstore_info efi_pstore_info;
  865. #ifdef CONFIG_PSTORE
  866. static int efi_pstore_open(struct pstore_info *psi)
  867. {
  868. struct efivars *efivars = psi->data;
  869. spin_lock(&efivars->lock);
  870. efivars->walk_entry = list_first_entry(&efivars->list,
  871. struct efivar_entry, list);
  872. return 0;
  873. }
  874. static int efi_pstore_close(struct pstore_info *psi)
  875. {
  876. struct efivars *efivars = psi->data;
  877. spin_unlock(&efivars->lock);
  878. return 0;
  879. }
  880. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  881. struct timespec *timespec,
  882. char **buf, struct pstore_info *psi)
  883. {
  884. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  885. struct efivars *efivars = psi->data;
  886. char name[DUMP_NAME_LEN];
  887. int i;
  888. unsigned int part, size;
  889. unsigned long time;
  890. while (&efivars->walk_entry->list != &efivars->list) {
  891. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  892. vendor)) {
  893. for (i = 0; i < DUMP_NAME_LEN; i++) {
  894. name[i] = efivars->walk_entry->var.VariableName[i];
  895. }
  896. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  897. *id = part;
  898. timespec->tv_sec = time;
  899. timespec->tv_nsec = 0;
  900. get_var_data_locked(efivars, &efivars->walk_entry->var);
  901. size = efivars->walk_entry->var.DataSize;
  902. *buf = kmalloc(size, GFP_KERNEL);
  903. if (*buf == NULL)
  904. return -ENOMEM;
  905. memcpy(*buf, efivars->walk_entry->var.Data,
  906. size);
  907. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  908. struct efivar_entry, list);
  909. return size;
  910. }
  911. }
  912. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  913. struct efivar_entry, list);
  914. }
  915. return 0;
  916. }
  917. static int efi_pstore_write(enum pstore_type_id type,
  918. enum kmsg_dump_reason reason, u64 *id,
  919. unsigned int part, size_t size, struct pstore_info *psi)
  920. {
  921. char name[DUMP_NAME_LEN];
  922. char stub_name[DUMP_NAME_LEN];
  923. efi_char16_t efi_name[DUMP_NAME_LEN];
  924. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  925. struct efivars *efivars = psi->data;
  926. struct efivar_entry *entry, *found = NULL;
  927. int i, ret = 0;
  928. sprintf(stub_name, "dump-type%u-%u-", type, part);
  929. sprintf(name, "%s%lu", stub_name, get_seconds());
  930. spin_lock(&efivars->lock);
  931. for (i = 0; i < DUMP_NAME_LEN; i++)
  932. efi_name[i] = stub_name[i];
  933. /*
  934. * Clean up any entries with the same name
  935. */
  936. list_for_each_entry(entry, &efivars->list, list) {
  937. get_var_data_locked(efivars, &entry->var);
  938. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  939. continue;
  940. if (utf16_strncmp(entry->var.VariableName, efi_name,
  941. utf16_strlen(efi_name)))
  942. continue;
  943. /* Needs to be a prefix */
  944. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  945. continue;
  946. /* found */
  947. found = entry;
  948. efivars->ops->set_variable(entry->var.VariableName,
  949. &entry->var.VendorGuid,
  950. PSTORE_EFI_ATTRIBUTES,
  951. 0, NULL);
  952. }
  953. if (found)
  954. list_del(&found->list);
  955. for (i = 0; i < DUMP_NAME_LEN; i++)
  956. efi_name[i] = name[i];
  957. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  958. size, psi->buf);
  959. spin_unlock(&efivars->lock);
  960. if (found)
  961. efivar_unregister(found);
  962. if (size)
  963. ret = efivar_create_sysfs_entry(efivars,
  964. utf16_strsize(efi_name,
  965. DUMP_NAME_LEN * 2),
  966. efi_name, &vendor);
  967. *id = part;
  968. return ret;
  969. };
  970. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  971. struct pstore_info *psi)
  972. {
  973. efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
  974. return 0;
  975. }
  976. #else
  977. static int efi_pstore_open(struct pstore_info *psi)
  978. {
  979. return 0;
  980. }
  981. static int efi_pstore_close(struct pstore_info *psi)
  982. {
  983. return 0;
  984. }
  985. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  986. struct timespec *timespec,
  987. char **buf, struct pstore_info *psi)
  988. {
  989. return -1;
  990. }
  991. static int efi_pstore_write(enum pstore_type_id type,
  992. enum kmsg_dump_reason reason, u64 *id,
  993. unsigned int part, size_t size, struct pstore_info *psi)
  994. {
  995. return 0;
  996. }
  997. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  998. struct pstore_info *psi)
  999. {
  1000. return 0;
  1001. }
  1002. #endif
  1003. static struct pstore_info efi_pstore_info = {
  1004. .owner = THIS_MODULE,
  1005. .name = "efi",
  1006. .open = efi_pstore_open,
  1007. .close = efi_pstore_close,
  1008. .read = efi_pstore_read,
  1009. .write = efi_pstore_write,
  1010. .erase = efi_pstore_erase,
  1011. };
  1012. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  1013. struct bin_attribute *bin_attr,
  1014. char *buf, loff_t pos, size_t count)
  1015. {
  1016. struct efi_variable *new_var = (struct efi_variable *)buf;
  1017. struct efivars *efivars = bin_attr->private;
  1018. struct efivar_entry *search_efivar, *n;
  1019. unsigned long strsize1, strsize2;
  1020. efi_status_t status = EFI_NOT_FOUND;
  1021. int found = 0;
  1022. if (!capable(CAP_SYS_ADMIN))
  1023. return -EACCES;
  1024. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  1025. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  1026. printk(KERN_ERR "efivars: Malformed variable content\n");
  1027. return -EINVAL;
  1028. }
  1029. spin_lock(&efivars->lock);
  1030. /*
  1031. * Does this variable already exist?
  1032. */
  1033. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1034. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1035. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  1036. if (strsize1 == strsize2 &&
  1037. !memcmp(&(search_efivar->var.VariableName),
  1038. new_var->VariableName, strsize1) &&
  1039. !efi_guidcmp(search_efivar->var.VendorGuid,
  1040. new_var->VendorGuid)) {
  1041. found = 1;
  1042. break;
  1043. }
  1044. }
  1045. if (found) {
  1046. spin_unlock(&efivars->lock);
  1047. return -EINVAL;
  1048. }
  1049. /* now *really* create the variable via EFI */
  1050. status = efivars->ops->set_variable(new_var->VariableName,
  1051. &new_var->VendorGuid,
  1052. new_var->Attributes,
  1053. new_var->DataSize,
  1054. new_var->Data);
  1055. if (status != EFI_SUCCESS) {
  1056. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1057. status);
  1058. spin_unlock(&efivars->lock);
  1059. return -EIO;
  1060. }
  1061. spin_unlock(&efivars->lock);
  1062. /* Create the entry in sysfs. Locking is not required here */
  1063. status = efivar_create_sysfs_entry(efivars,
  1064. utf16_strsize(new_var->VariableName,
  1065. 1024),
  1066. new_var->VariableName,
  1067. &new_var->VendorGuid);
  1068. if (status) {
  1069. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  1070. }
  1071. return count;
  1072. }
  1073. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  1074. struct bin_attribute *bin_attr,
  1075. char *buf, loff_t pos, size_t count)
  1076. {
  1077. struct efi_variable *del_var = (struct efi_variable *)buf;
  1078. struct efivars *efivars = bin_attr->private;
  1079. struct efivar_entry *search_efivar, *n;
  1080. unsigned long strsize1, strsize2;
  1081. efi_status_t status = EFI_NOT_FOUND;
  1082. int found = 0;
  1083. if (!capable(CAP_SYS_ADMIN))
  1084. return -EACCES;
  1085. spin_lock(&efivars->lock);
  1086. /*
  1087. * Does this variable already exist?
  1088. */
  1089. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1090. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1091. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  1092. if (strsize1 == strsize2 &&
  1093. !memcmp(&(search_efivar->var.VariableName),
  1094. del_var->VariableName, strsize1) &&
  1095. !efi_guidcmp(search_efivar->var.VendorGuid,
  1096. del_var->VendorGuid)) {
  1097. found = 1;
  1098. break;
  1099. }
  1100. }
  1101. if (!found) {
  1102. spin_unlock(&efivars->lock);
  1103. return -EINVAL;
  1104. }
  1105. /* force the Attributes/DataSize to 0 to ensure deletion */
  1106. del_var->Attributes = 0;
  1107. del_var->DataSize = 0;
  1108. status = efivars->ops->set_variable(del_var->VariableName,
  1109. &del_var->VendorGuid,
  1110. del_var->Attributes,
  1111. del_var->DataSize,
  1112. del_var->Data);
  1113. if (status != EFI_SUCCESS) {
  1114. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1115. status);
  1116. spin_unlock(&efivars->lock);
  1117. return -EIO;
  1118. }
  1119. list_del(&search_efivar->list);
  1120. /* We need to release this lock before unregistering. */
  1121. spin_unlock(&efivars->lock);
  1122. efivar_unregister(search_efivar);
  1123. /* It's dead Jim.... */
  1124. return count;
  1125. }
  1126. /*
  1127. * Let's not leave out systab information that snuck into
  1128. * the efivars driver
  1129. */
  1130. static ssize_t systab_show(struct kobject *kobj,
  1131. struct kobj_attribute *attr, char *buf)
  1132. {
  1133. char *str = buf;
  1134. if (!kobj || !buf)
  1135. return -EINVAL;
  1136. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1137. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1138. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1139. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1140. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1141. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1142. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1143. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1144. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1145. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1146. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1147. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1148. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1149. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1150. return str - buf;
  1151. }
  1152. static struct kobj_attribute efi_attr_systab =
  1153. __ATTR(systab, 0400, systab_show, NULL);
  1154. static struct attribute *efi_subsys_attrs[] = {
  1155. &efi_attr_systab.attr,
  1156. NULL, /* maybe more in the future? */
  1157. };
  1158. static struct attribute_group efi_subsys_attr_group = {
  1159. .attrs = efi_subsys_attrs,
  1160. };
  1161. static struct kobject *efi_kobj;
  1162. /*
  1163. * efivar_create_sysfs_entry()
  1164. * Requires:
  1165. * variable_name_size = number of bytes required to hold
  1166. * variable_name (not counting the NULL
  1167. * character at the end.
  1168. * efivars->lock is not held on entry or exit.
  1169. * Returns 1 on failure, 0 on success
  1170. */
  1171. static int
  1172. efivar_create_sysfs_entry(struct efivars *efivars,
  1173. unsigned long variable_name_size,
  1174. efi_char16_t *variable_name,
  1175. efi_guid_t *vendor_guid)
  1176. {
  1177. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  1178. char *short_name;
  1179. struct efivar_entry *new_efivar;
  1180. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  1181. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  1182. if (!short_name || !new_efivar) {
  1183. kfree(short_name);
  1184. kfree(new_efivar);
  1185. return 1;
  1186. }
  1187. new_efivar->efivars = efivars;
  1188. memcpy(new_efivar->var.VariableName, variable_name,
  1189. variable_name_size);
  1190. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1191. /* Convert Unicode to normal chars (assume top bits are 0),
  1192. ala UTF-8 */
  1193. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1194. short_name[i] = variable_name[i] & 0xFF;
  1195. }
  1196. /* This is ugly, but necessary to separate one vendor's
  1197. private variables from another's. */
  1198. *(short_name + strlen(short_name)) = '-';
  1199. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1200. new_efivar->kobj.kset = efivars->kset;
  1201. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1202. "%s", short_name);
  1203. if (i) {
  1204. kfree(short_name);
  1205. kfree(new_efivar);
  1206. return 1;
  1207. }
  1208. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1209. kfree(short_name);
  1210. short_name = NULL;
  1211. spin_lock(&efivars->lock);
  1212. list_add(&new_efivar->list, &efivars->list);
  1213. spin_unlock(&efivars->lock);
  1214. return 0;
  1215. }
  1216. static int
  1217. create_efivars_bin_attributes(struct efivars *efivars)
  1218. {
  1219. struct bin_attribute *attr;
  1220. int error;
  1221. /* new_var */
  1222. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1223. if (!attr)
  1224. return -ENOMEM;
  1225. attr->attr.name = "new_var";
  1226. attr->attr.mode = 0200;
  1227. attr->write = efivar_create;
  1228. attr->private = efivars;
  1229. efivars->new_var = attr;
  1230. /* del_var */
  1231. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1232. if (!attr) {
  1233. error = -ENOMEM;
  1234. goto out_free;
  1235. }
  1236. attr->attr.name = "del_var";
  1237. attr->attr.mode = 0200;
  1238. attr->write = efivar_delete;
  1239. attr->private = efivars;
  1240. efivars->del_var = attr;
  1241. sysfs_bin_attr_init(efivars->new_var);
  1242. sysfs_bin_attr_init(efivars->del_var);
  1243. /* Register */
  1244. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1245. efivars->new_var);
  1246. if (error) {
  1247. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1248. " due to error %d\n", error);
  1249. goto out_free;
  1250. }
  1251. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1252. efivars->del_var);
  1253. if (error) {
  1254. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1255. " due to error %d\n", error);
  1256. sysfs_remove_bin_file(&efivars->kset->kobj,
  1257. efivars->new_var);
  1258. goto out_free;
  1259. }
  1260. return 0;
  1261. out_free:
  1262. kfree(efivars->del_var);
  1263. efivars->del_var = NULL;
  1264. kfree(efivars->new_var);
  1265. efivars->new_var = NULL;
  1266. return error;
  1267. }
  1268. void unregister_efivars(struct efivars *efivars)
  1269. {
  1270. struct efivar_entry *entry, *n;
  1271. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1272. spin_lock(&efivars->lock);
  1273. list_del(&entry->list);
  1274. spin_unlock(&efivars->lock);
  1275. efivar_unregister(entry);
  1276. }
  1277. if (efivars->new_var)
  1278. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1279. if (efivars->del_var)
  1280. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1281. kfree(efivars->new_var);
  1282. kfree(efivars->del_var);
  1283. kobject_put(efivars->kobject);
  1284. kset_unregister(efivars->kset);
  1285. }
  1286. EXPORT_SYMBOL_GPL(unregister_efivars);
  1287. int register_efivars(struct efivars *efivars,
  1288. const struct efivar_operations *ops,
  1289. struct kobject *parent_kobj)
  1290. {
  1291. efi_status_t status = EFI_NOT_FOUND;
  1292. efi_guid_t vendor_guid;
  1293. efi_char16_t *variable_name;
  1294. unsigned long variable_name_size = 1024;
  1295. int error = 0;
  1296. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1297. if (!variable_name) {
  1298. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1299. return -ENOMEM;
  1300. }
  1301. spin_lock_init(&efivars->lock);
  1302. INIT_LIST_HEAD(&efivars->list);
  1303. efivars->ops = ops;
  1304. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1305. if (!efivars->kset) {
  1306. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1307. error = -ENOMEM;
  1308. goto out;
  1309. }
  1310. efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
  1311. if (!efivars->kobject) {
  1312. pr_err("efivars: Subsystem registration failed.\n");
  1313. error = -ENOMEM;
  1314. kset_unregister(efivars->kset);
  1315. goto out;
  1316. }
  1317. /*
  1318. * Per EFI spec, the maximum storage allocated for both
  1319. * the variable name and variable data is 1024 bytes.
  1320. */
  1321. do {
  1322. variable_name_size = 1024;
  1323. status = ops->get_next_variable(&variable_name_size,
  1324. variable_name,
  1325. &vendor_guid);
  1326. switch (status) {
  1327. case EFI_SUCCESS:
  1328. efivar_create_sysfs_entry(efivars,
  1329. variable_name_size,
  1330. variable_name,
  1331. &vendor_guid);
  1332. break;
  1333. case EFI_NOT_FOUND:
  1334. break;
  1335. default:
  1336. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1337. status);
  1338. status = EFI_NOT_FOUND;
  1339. break;
  1340. }
  1341. } while (status != EFI_NOT_FOUND);
  1342. error = create_efivars_bin_attributes(efivars);
  1343. if (error)
  1344. unregister_efivars(efivars);
  1345. efivars->efi_pstore_info = efi_pstore_info;
  1346. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1347. if (efivars->efi_pstore_info.buf) {
  1348. efivars->efi_pstore_info.bufsize = 1024;
  1349. efivars->efi_pstore_info.data = efivars;
  1350. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1351. pstore_register(&efivars->efi_pstore_info);
  1352. }
  1353. register_filesystem(&efivarfs_type);
  1354. out:
  1355. kfree(variable_name);
  1356. return error;
  1357. }
  1358. EXPORT_SYMBOL_GPL(register_efivars);
  1359. /*
  1360. * For now we register the efi subsystem with the firmware subsystem
  1361. * and the vars subsystem with the efi subsystem. In the future, it
  1362. * might make sense to split off the efi subsystem into its own
  1363. * driver, but for now only efivars will register with it, so just
  1364. * include it here.
  1365. */
  1366. static int __init
  1367. efivars_init(void)
  1368. {
  1369. int error = 0;
  1370. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1371. EFIVARS_DATE);
  1372. if (!efi_enabled)
  1373. return 0;
  1374. /* For now we'll register the efi directory at /sys/firmware/efi */
  1375. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1376. if (!efi_kobj) {
  1377. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1378. return -ENOMEM;
  1379. }
  1380. ops.get_variable = efi.get_variable;
  1381. ops.set_variable = efi.set_variable;
  1382. ops.get_next_variable = efi.get_next_variable;
  1383. error = register_efivars(&__efivars, &ops, efi_kobj);
  1384. if (error)
  1385. goto err_put;
  1386. /* Don't forget the systab entry */
  1387. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1388. if (error) {
  1389. printk(KERN_ERR
  1390. "efivars: Sysfs attribute export failed with error %d.\n",
  1391. error);
  1392. goto err_unregister;
  1393. }
  1394. return 0;
  1395. err_unregister:
  1396. unregister_efivars(&__efivars);
  1397. err_put:
  1398. kobject_put(efi_kobj);
  1399. return error;
  1400. }
  1401. static void __exit
  1402. efivars_exit(void)
  1403. {
  1404. if (efi_enabled) {
  1405. unregister_efivars(&__efivars);
  1406. kobject_put(efi_kobj);
  1407. }
  1408. }
  1409. module_init(efivars_init);
  1410. module_exit(efivars_exit);