efivars.c 43 KB

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