efivars.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  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. ssize_t bytes = 0;
  591. if (count < sizeof(attributes))
  592. return -EINVAL;
  593. data = kmalloc(datasize, GFP_KERNEL);
  594. if (!data)
  595. return -ENOMEM;
  596. efivars = var->efivars;
  597. if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
  598. bytes = -EFAULT;
  599. goto out;
  600. }
  601. if (attributes & ~(EFI_VARIABLE_MASK)) {
  602. bytes = -EINVAL;
  603. goto out;
  604. }
  605. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  606. bytes = -EFAULT;
  607. goto out;
  608. }
  609. if (validate_var(&var->var, data, datasize) == false) {
  610. bytes = -EINVAL;
  611. goto out;
  612. }
  613. /*
  614. * The lock here protects the get_variable call, the conditional
  615. * set_variable call, and removal of the variable from the efivars
  616. * list (in the case of an authenticated delete).
  617. */
  618. spin_lock(&efivars->lock);
  619. status = efivars->ops->set_variable(var->var.VariableName,
  620. &var->var.VendorGuid,
  621. attributes, datasize,
  622. data);
  623. if (status != EFI_SUCCESS) {
  624. spin_unlock(&efivars->lock);
  625. kfree(data);
  626. return efi_status_to_err(status);
  627. }
  628. bytes = count;
  629. /*
  630. * Writing to the variable may have caused a change in size (which
  631. * could either be an append or an overwrite), or the variable to be
  632. * deleted. Perform a GetVariable() so we can tell what actually
  633. * happened.
  634. */
  635. newdatasize = 0;
  636. status = efivars->ops->get_variable(var->var.VariableName,
  637. &var->var.VendorGuid,
  638. NULL, &newdatasize,
  639. NULL);
  640. if (status == EFI_BUFFER_TOO_SMALL) {
  641. spin_unlock(&efivars->lock);
  642. mutex_lock(&inode->i_mutex);
  643. i_size_write(inode, newdatasize + sizeof(attributes));
  644. mutex_unlock(&inode->i_mutex);
  645. } else if (status == EFI_NOT_FOUND) {
  646. list_del(&var->list);
  647. spin_unlock(&efivars->lock);
  648. efivar_unregister(var);
  649. drop_nlink(inode);
  650. dput(file->f_dentry);
  651. } else {
  652. spin_unlock(&efivars->lock);
  653. pr_warn("efivarfs: inconsistent EFI variable implementation? "
  654. "status = %lx\n", status);
  655. }
  656. out:
  657. kfree(data);
  658. return bytes;
  659. }
  660. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  661. size_t count, loff_t *ppos)
  662. {
  663. struct efivar_entry *var = file->private_data;
  664. struct efivars *efivars = var->efivars;
  665. efi_status_t status;
  666. unsigned long datasize = 0;
  667. u32 attributes;
  668. void *data;
  669. ssize_t size = 0;
  670. spin_lock(&efivars->lock);
  671. status = efivars->ops->get_variable(var->var.VariableName,
  672. &var->var.VendorGuid,
  673. &attributes, &datasize, NULL);
  674. spin_unlock(&efivars->lock);
  675. if (status != EFI_BUFFER_TOO_SMALL)
  676. return efi_status_to_err(status);
  677. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  678. if (!data)
  679. return -ENOMEM;
  680. spin_lock(&efivars->lock);
  681. status = efivars->ops->get_variable(var->var.VariableName,
  682. &var->var.VendorGuid,
  683. &attributes, &datasize,
  684. (data + sizeof(attributes)));
  685. spin_unlock(&efivars->lock);
  686. if (status != EFI_SUCCESS) {
  687. size = efi_status_to_err(status);
  688. goto out_free;
  689. }
  690. memcpy(data, &attributes, sizeof(attributes));
  691. size = simple_read_from_buffer(userbuf, count, ppos,
  692. data, datasize + sizeof(attributes));
  693. out_free:
  694. kfree(data);
  695. return size;
  696. }
  697. static void efivarfs_evict_inode(struct inode *inode)
  698. {
  699. clear_inode(inode);
  700. }
  701. static const struct super_operations efivarfs_ops = {
  702. .statfs = simple_statfs,
  703. .drop_inode = generic_delete_inode,
  704. .evict_inode = efivarfs_evict_inode,
  705. .show_options = generic_show_options,
  706. };
  707. static struct super_block *efivarfs_sb;
  708. static const struct inode_operations efivarfs_dir_inode_operations;
  709. static const struct file_operations efivarfs_file_operations = {
  710. .open = efivarfs_file_open,
  711. .read = efivarfs_file_read,
  712. .write = efivarfs_file_write,
  713. .llseek = no_llseek,
  714. };
  715. static struct inode *efivarfs_get_inode(struct super_block *sb,
  716. const struct inode *dir, int mode, dev_t dev)
  717. {
  718. struct inode *inode = new_inode(sb);
  719. if (inode) {
  720. inode->i_ino = get_next_ino();
  721. inode->i_uid = inode->i_gid = 0;
  722. inode->i_mode = mode;
  723. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  724. switch (mode & S_IFMT) {
  725. case S_IFREG:
  726. inode->i_fop = &efivarfs_file_operations;
  727. break;
  728. case S_IFDIR:
  729. inode->i_op = &efivarfs_dir_inode_operations;
  730. inode->i_fop = &simple_dir_operations;
  731. inc_nlink(inode);
  732. break;
  733. }
  734. }
  735. return inode;
  736. }
  737. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  738. {
  739. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  740. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  741. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  742. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  743. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  744. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  745. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  746. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  747. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  748. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  749. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  750. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  751. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  752. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  753. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  754. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  755. }
  756. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  757. umode_t mode, bool excl)
  758. {
  759. struct inode *inode;
  760. struct efivars *efivars = &__efivars;
  761. struct efivar_entry *var;
  762. int namelen, i = 0, err = 0;
  763. /*
  764. * We need a GUID, plus at least one letter for the variable name,
  765. * plus the '-' separator
  766. */
  767. if (dentry->d_name.len < GUID_LEN + 2)
  768. return -EINVAL;
  769. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  770. if (!inode)
  771. return -ENOMEM;
  772. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  773. if (!var) {
  774. err = -ENOMEM;
  775. goto out;
  776. }
  777. /* length of the variable name itself: remove GUID and separator */
  778. namelen = dentry->d_name.len - GUID_LEN - 1;
  779. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  780. &var->var.VendorGuid);
  781. for (i = 0; i < namelen; i++)
  782. var->var.VariableName[i] = dentry->d_name.name[i];
  783. var->var.VariableName[i] = '\0';
  784. inode->i_private = var;
  785. var->efivars = efivars;
  786. var->kobj.kset = efivars->kset;
  787. err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
  788. dentry->d_name.name);
  789. if (err)
  790. goto out;
  791. kobject_uevent(&var->kobj, KOBJ_ADD);
  792. spin_lock(&efivars->lock);
  793. list_add(&var->list, &efivars->list);
  794. spin_unlock(&efivars->lock);
  795. d_instantiate(dentry, inode);
  796. dget(dentry);
  797. out:
  798. if (err) {
  799. kfree(var);
  800. iput(inode);
  801. }
  802. return err;
  803. }
  804. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  805. {
  806. struct efivar_entry *var = dentry->d_inode->i_private;
  807. struct efivars *efivars = var->efivars;
  808. efi_status_t status;
  809. spin_lock(&efivars->lock);
  810. status = efivars->ops->set_variable(var->var.VariableName,
  811. &var->var.VendorGuid,
  812. 0, 0, NULL);
  813. if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
  814. list_del(&var->list);
  815. spin_unlock(&efivars->lock);
  816. efivar_unregister(var);
  817. drop_nlink(dir);
  818. dput(dentry);
  819. return 0;
  820. }
  821. spin_unlock(&efivars->lock);
  822. return -EINVAL;
  823. };
  824. int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  825. {
  826. struct inode *inode = NULL;
  827. struct dentry *root;
  828. struct efivar_entry *entry, *n;
  829. struct efivars *efivars = &__efivars;
  830. char *name;
  831. efivarfs_sb = sb;
  832. sb->s_maxbytes = MAX_LFS_FILESIZE;
  833. sb->s_blocksize = PAGE_CACHE_SIZE;
  834. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  835. sb->s_magic = EFIVARFS_MAGIC;
  836. sb->s_op = &efivarfs_ops;
  837. sb->s_time_gran = 1;
  838. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  839. if (!inode)
  840. return -ENOMEM;
  841. inode->i_op = &efivarfs_dir_inode_operations;
  842. root = d_make_root(inode);
  843. sb->s_root = root;
  844. if (!root)
  845. return -ENOMEM;
  846. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  847. struct dentry *dentry, *root = efivarfs_sb->s_root;
  848. unsigned long size = 0;
  849. int len, i;
  850. inode = NULL;
  851. len = utf16_strlen(entry->var.VariableName);
  852. /* name, plus '-', plus GUID, plus NUL*/
  853. name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
  854. if (!name)
  855. goto fail;
  856. for (i = 0; i < len; i++)
  857. name[i] = entry->var.VariableName[i] & 0xFF;
  858. name[len] = '-';
  859. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  860. name[len+GUID_LEN+1] = '\0';
  861. inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
  862. S_IFREG | 0644, 0);
  863. if (!inode)
  864. goto fail_name;
  865. dentry = d_alloc_name(root, name);
  866. if (!dentry)
  867. goto fail_inode;
  868. /* copied by the above to local storage in the dentry. */
  869. kfree(name);
  870. spin_lock(&efivars->lock);
  871. efivars->ops->get_variable(entry->var.VariableName,
  872. &entry->var.VendorGuid,
  873. &entry->var.Attributes,
  874. &size,
  875. NULL);
  876. spin_unlock(&efivars->lock);
  877. mutex_lock(&inode->i_mutex);
  878. inode->i_private = entry;
  879. i_size_write(inode, size+4);
  880. mutex_unlock(&inode->i_mutex);
  881. d_add(dentry, inode);
  882. }
  883. return 0;
  884. fail_inode:
  885. iput(inode);
  886. fail_name:
  887. kfree(name);
  888. fail:
  889. return -ENOMEM;
  890. }
  891. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  892. int flags, const char *dev_name, void *data)
  893. {
  894. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  895. }
  896. static void efivarfs_kill_sb(struct super_block *sb)
  897. {
  898. kill_litter_super(sb);
  899. efivarfs_sb = NULL;
  900. }
  901. static struct file_system_type efivarfs_type = {
  902. .name = "efivarfs",
  903. .mount = efivarfs_mount,
  904. .kill_sb = efivarfs_kill_sb,
  905. };
  906. static const struct inode_operations efivarfs_dir_inode_operations = {
  907. .lookup = simple_lookup,
  908. .unlink = efivarfs_unlink,
  909. .create = efivarfs_create,
  910. };
  911. static struct pstore_info efi_pstore_info;
  912. #ifdef CONFIG_PSTORE
  913. static int efi_pstore_open(struct pstore_info *psi)
  914. {
  915. struct efivars *efivars = psi->data;
  916. spin_lock(&efivars->lock);
  917. efivars->walk_entry = list_first_entry(&efivars->list,
  918. struct efivar_entry, list);
  919. return 0;
  920. }
  921. static int efi_pstore_close(struct pstore_info *psi)
  922. {
  923. struct efivars *efivars = psi->data;
  924. spin_unlock(&efivars->lock);
  925. return 0;
  926. }
  927. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  928. struct timespec *timespec,
  929. char **buf, struct pstore_info *psi)
  930. {
  931. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  932. struct efivars *efivars = psi->data;
  933. char name[DUMP_NAME_LEN];
  934. int i;
  935. unsigned int part, size;
  936. unsigned long time;
  937. while (&efivars->walk_entry->list != &efivars->list) {
  938. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  939. vendor)) {
  940. for (i = 0; i < DUMP_NAME_LEN; i++) {
  941. name[i] = efivars->walk_entry->var.VariableName[i];
  942. }
  943. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  944. *id = part;
  945. timespec->tv_sec = time;
  946. timespec->tv_nsec = 0;
  947. get_var_data_locked(efivars, &efivars->walk_entry->var);
  948. size = efivars->walk_entry->var.DataSize;
  949. *buf = kmalloc(size, GFP_KERNEL);
  950. if (*buf == NULL)
  951. return -ENOMEM;
  952. memcpy(*buf, efivars->walk_entry->var.Data,
  953. size);
  954. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  955. struct efivar_entry, list);
  956. return size;
  957. }
  958. }
  959. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  960. struct efivar_entry, list);
  961. }
  962. return 0;
  963. }
  964. static int efi_pstore_write(enum pstore_type_id type,
  965. enum kmsg_dump_reason reason, u64 *id,
  966. unsigned int part, size_t size, struct pstore_info *psi)
  967. {
  968. char name[DUMP_NAME_LEN];
  969. char stub_name[DUMP_NAME_LEN];
  970. efi_char16_t efi_name[DUMP_NAME_LEN];
  971. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  972. struct efivars *efivars = psi->data;
  973. struct efivar_entry *entry, *found = NULL;
  974. int i, ret = 0;
  975. sprintf(stub_name, "dump-type%u-%u-", type, part);
  976. sprintf(name, "%s%lu", stub_name, get_seconds());
  977. spin_lock(&efivars->lock);
  978. for (i = 0; i < DUMP_NAME_LEN; i++)
  979. efi_name[i] = stub_name[i];
  980. /*
  981. * Clean up any entries with the same name
  982. */
  983. list_for_each_entry(entry, &efivars->list, list) {
  984. get_var_data_locked(efivars, &entry->var);
  985. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  986. continue;
  987. if (utf16_strncmp(entry->var.VariableName, efi_name,
  988. utf16_strlen(efi_name)))
  989. continue;
  990. /* Needs to be a prefix */
  991. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  992. continue;
  993. /* found */
  994. found = entry;
  995. efivars->ops->set_variable(entry->var.VariableName,
  996. &entry->var.VendorGuid,
  997. PSTORE_EFI_ATTRIBUTES,
  998. 0, NULL);
  999. }
  1000. if (found)
  1001. list_del(&found->list);
  1002. for (i = 0; i < DUMP_NAME_LEN; i++)
  1003. efi_name[i] = name[i];
  1004. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  1005. size, psi->buf);
  1006. spin_unlock(&efivars->lock);
  1007. if (found)
  1008. efivar_unregister(found);
  1009. if (size)
  1010. ret = efivar_create_sysfs_entry(efivars,
  1011. utf16_strsize(efi_name,
  1012. DUMP_NAME_LEN * 2),
  1013. efi_name, &vendor);
  1014. *id = part;
  1015. return ret;
  1016. };
  1017. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  1018. struct pstore_info *psi)
  1019. {
  1020. efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
  1021. return 0;
  1022. }
  1023. #else
  1024. static int efi_pstore_open(struct pstore_info *psi)
  1025. {
  1026. return 0;
  1027. }
  1028. static int efi_pstore_close(struct pstore_info *psi)
  1029. {
  1030. return 0;
  1031. }
  1032. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  1033. struct timespec *timespec,
  1034. char **buf, struct pstore_info *psi)
  1035. {
  1036. return -1;
  1037. }
  1038. static int efi_pstore_write(enum pstore_type_id type,
  1039. enum kmsg_dump_reason reason, u64 *id,
  1040. unsigned int part, size_t size, struct pstore_info *psi)
  1041. {
  1042. return 0;
  1043. }
  1044. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  1045. struct pstore_info *psi)
  1046. {
  1047. return 0;
  1048. }
  1049. #endif
  1050. static struct pstore_info efi_pstore_info = {
  1051. .owner = THIS_MODULE,
  1052. .name = "efi",
  1053. .open = efi_pstore_open,
  1054. .close = efi_pstore_close,
  1055. .read = efi_pstore_read,
  1056. .write = efi_pstore_write,
  1057. .erase = efi_pstore_erase,
  1058. };
  1059. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  1060. struct bin_attribute *bin_attr,
  1061. char *buf, loff_t pos, size_t count)
  1062. {
  1063. struct efi_variable *new_var = (struct efi_variable *)buf;
  1064. struct efivars *efivars = bin_attr->private;
  1065. struct efivar_entry *search_efivar, *n;
  1066. unsigned long strsize1, strsize2;
  1067. efi_status_t status = EFI_NOT_FOUND;
  1068. int found = 0;
  1069. if (!capable(CAP_SYS_ADMIN))
  1070. return -EACCES;
  1071. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  1072. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  1073. printk(KERN_ERR "efivars: Malformed variable content\n");
  1074. return -EINVAL;
  1075. }
  1076. spin_lock(&efivars->lock);
  1077. /*
  1078. * Does this variable already exist?
  1079. */
  1080. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1081. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1082. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  1083. if (strsize1 == strsize2 &&
  1084. !memcmp(&(search_efivar->var.VariableName),
  1085. new_var->VariableName, strsize1) &&
  1086. !efi_guidcmp(search_efivar->var.VendorGuid,
  1087. new_var->VendorGuid)) {
  1088. found = 1;
  1089. break;
  1090. }
  1091. }
  1092. if (found) {
  1093. spin_unlock(&efivars->lock);
  1094. return -EINVAL;
  1095. }
  1096. /* now *really* create the variable via EFI */
  1097. status = efivars->ops->set_variable(new_var->VariableName,
  1098. &new_var->VendorGuid,
  1099. new_var->Attributes,
  1100. new_var->DataSize,
  1101. new_var->Data);
  1102. if (status != EFI_SUCCESS) {
  1103. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1104. status);
  1105. spin_unlock(&efivars->lock);
  1106. return -EIO;
  1107. }
  1108. spin_unlock(&efivars->lock);
  1109. /* Create the entry in sysfs. Locking is not required here */
  1110. status = efivar_create_sysfs_entry(efivars,
  1111. utf16_strsize(new_var->VariableName,
  1112. 1024),
  1113. new_var->VariableName,
  1114. &new_var->VendorGuid);
  1115. if (status) {
  1116. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  1117. }
  1118. return count;
  1119. }
  1120. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  1121. struct bin_attribute *bin_attr,
  1122. char *buf, loff_t pos, size_t count)
  1123. {
  1124. struct efi_variable *del_var = (struct efi_variable *)buf;
  1125. struct efivars *efivars = bin_attr->private;
  1126. struct efivar_entry *search_efivar, *n;
  1127. unsigned long strsize1, strsize2;
  1128. efi_status_t status = EFI_NOT_FOUND;
  1129. int found = 0;
  1130. if (!capable(CAP_SYS_ADMIN))
  1131. return -EACCES;
  1132. spin_lock(&efivars->lock);
  1133. /*
  1134. * Does this variable already exist?
  1135. */
  1136. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1137. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1138. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  1139. if (strsize1 == strsize2 &&
  1140. !memcmp(&(search_efivar->var.VariableName),
  1141. del_var->VariableName, strsize1) &&
  1142. !efi_guidcmp(search_efivar->var.VendorGuid,
  1143. del_var->VendorGuid)) {
  1144. found = 1;
  1145. break;
  1146. }
  1147. }
  1148. if (!found) {
  1149. spin_unlock(&efivars->lock);
  1150. return -EINVAL;
  1151. }
  1152. /* force the Attributes/DataSize to 0 to ensure deletion */
  1153. del_var->Attributes = 0;
  1154. del_var->DataSize = 0;
  1155. status = efivars->ops->set_variable(del_var->VariableName,
  1156. &del_var->VendorGuid,
  1157. del_var->Attributes,
  1158. del_var->DataSize,
  1159. del_var->Data);
  1160. if (status != EFI_SUCCESS) {
  1161. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1162. status);
  1163. spin_unlock(&efivars->lock);
  1164. return -EIO;
  1165. }
  1166. list_del(&search_efivar->list);
  1167. /* We need to release this lock before unregistering. */
  1168. spin_unlock(&efivars->lock);
  1169. efivar_unregister(search_efivar);
  1170. /* It's dead Jim.... */
  1171. return count;
  1172. }
  1173. /*
  1174. * Let's not leave out systab information that snuck into
  1175. * the efivars driver
  1176. */
  1177. static ssize_t systab_show(struct kobject *kobj,
  1178. struct kobj_attribute *attr, char *buf)
  1179. {
  1180. char *str = buf;
  1181. if (!kobj || !buf)
  1182. return -EINVAL;
  1183. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1184. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1185. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1186. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1187. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1188. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1189. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1190. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1191. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1192. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1193. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1194. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1195. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1196. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1197. return str - buf;
  1198. }
  1199. static struct kobj_attribute efi_attr_systab =
  1200. __ATTR(systab, 0400, systab_show, NULL);
  1201. static struct attribute *efi_subsys_attrs[] = {
  1202. &efi_attr_systab.attr,
  1203. NULL, /* maybe more in the future? */
  1204. };
  1205. static struct attribute_group efi_subsys_attr_group = {
  1206. .attrs = efi_subsys_attrs,
  1207. };
  1208. static struct kobject *efi_kobj;
  1209. /*
  1210. * efivar_create_sysfs_entry()
  1211. * Requires:
  1212. * variable_name_size = number of bytes required to hold
  1213. * variable_name (not counting the NULL
  1214. * character at the end.
  1215. * efivars->lock is not held on entry or exit.
  1216. * Returns 1 on failure, 0 on success
  1217. */
  1218. static int
  1219. efivar_create_sysfs_entry(struct efivars *efivars,
  1220. unsigned long variable_name_size,
  1221. efi_char16_t *variable_name,
  1222. efi_guid_t *vendor_guid)
  1223. {
  1224. int i, short_name_size;
  1225. char *short_name;
  1226. struct efivar_entry *new_efivar;
  1227. /*
  1228. * Length of the variable bytes in ASCII, plus the '-' separator,
  1229. * plus the GUID, plus trailing NUL
  1230. */
  1231. short_name_size = variable_name_size / sizeof(efi_char16_t)
  1232. + 1 + GUID_LEN + 1;
  1233. short_name = kzalloc(short_name_size, GFP_KERNEL);
  1234. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  1235. if (!short_name || !new_efivar) {
  1236. kfree(short_name);
  1237. kfree(new_efivar);
  1238. return 1;
  1239. }
  1240. new_efivar->efivars = efivars;
  1241. memcpy(new_efivar->var.VariableName, variable_name,
  1242. variable_name_size);
  1243. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1244. /* Convert Unicode to normal chars (assume top bits are 0),
  1245. ala UTF-8 */
  1246. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1247. short_name[i] = variable_name[i] & 0xFF;
  1248. }
  1249. /* This is ugly, but necessary to separate one vendor's
  1250. private variables from another's. */
  1251. *(short_name + strlen(short_name)) = '-';
  1252. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1253. new_efivar->kobj.kset = efivars->kset;
  1254. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1255. "%s", short_name);
  1256. if (i) {
  1257. kfree(short_name);
  1258. kfree(new_efivar);
  1259. return 1;
  1260. }
  1261. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1262. kfree(short_name);
  1263. short_name = NULL;
  1264. spin_lock(&efivars->lock);
  1265. list_add(&new_efivar->list, &efivars->list);
  1266. spin_unlock(&efivars->lock);
  1267. return 0;
  1268. }
  1269. static int
  1270. create_efivars_bin_attributes(struct efivars *efivars)
  1271. {
  1272. struct bin_attribute *attr;
  1273. int error;
  1274. /* new_var */
  1275. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1276. if (!attr)
  1277. return -ENOMEM;
  1278. attr->attr.name = "new_var";
  1279. attr->attr.mode = 0200;
  1280. attr->write = efivar_create;
  1281. attr->private = efivars;
  1282. efivars->new_var = attr;
  1283. /* del_var */
  1284. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1285. if (!attr) {
  1286. error = -ENOMEM;
  1287. goto out_free;
  1288. }
  1289. attr->attr.name = "del_var";
  1290. attr->attr.mode = 0200;
  1291. attr->write = efivar_delete;
  1292. attr->private = efivars;
  1293. efivars->del_var = attr;
  1294. sysfs_bin_attr_init(efivars->new_var);
  1295. sysfs_bin_attr_init(efivars->del_var);
  1296. /* Register */
  1297. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1298. efivars->new_var);
  1299. if (error) {
  1300. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1301. " due to error %d\n", error);
  1302. goto out_free;
  1303. }
  1304. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1305. efivars->del_var);
  1306. if (error) {
  1307. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1308. " due to error %d\n", error);
  1309. sysfs_remove_bin_file(&efivars->kset->kobj,
  1310. efivars->new_var);
  1311. goto out_free;
  1312. }
  1313. return 0;
  1314. out_free:
  1315. kfree(efivars->del_var);
  1316. efivars->del_var = NULL;
  1317. kfree(efivars->new_var);
  1318. efivars->new_var = NULL;
  1319. return error;
  1320. }
  1321. void unregister_efivars(struct efivars *efivars)
  1322. {
  1323. struct efivar_entry *entry, *n;
  1324. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1325. spin_lock(&efivars->lock);
  1326. list_del(&entry->list);
  1327. spin_unlock(&efivars->lock);
  1328. efivar_unregister(entry);
  1329. }
  1330. if (efivars->new_var)
  1331. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1332. if (efivars->del_var)
  1333. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1334. kfree(efivars->new_var);
  1335. kfree(efivars->del_var);
  1336. kobject_put(efivars->kobject);
  1337. kset_unregister(efivars->kset);
  1338. }
  1339. EXPORT_SYMBOL_GPL(unregister_efivars);
  1340. int register_efivars(struct efivars *efivars,
  1341. const struct efivar_operations *ops,
  1342. struct kobject *parent_kobj)
  1343. {
  1344. efi_status_t status = EFI_NOT_FOUND;
  1345. efi_guid_t vendor_guid;
  1346. efi_char16_t *variable_name;
  1347. unsigned long variable_name_size = 1024;
  1348. int error = 0;
  1349. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1350. if (!variable_name) {
  1351. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1352. return -ENOMEM;
  1353. }
  1354. spin_lock_init(&efivars->lock);
  1355. INIT_LIST_HEAD(&efivars->list);
  1356. efivars->ops = ops;
  1357. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1358. if (!efivars->kset) {
  1359. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1360. error = -ENOMEM;
  1361. goto out;
  1362. }
  1363. efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
  1364. if (!efivars->kobject) {
  1365. pr_err("efivars: Subsystem registration failed.\n");
  1366. error = -ENOMEM;
  1367. kset_unregister(efivars->kset);
  1368. goto out;
  1369. }
  1370. /*
  1371. * Per EFI spec, the maximum storage allocated for both
  1372. * the variable name and variable data is 1024 bytes.
  1373. */
  1374. do {
  1375. variable_name_size = 1024;
  1376. status = ops->get_next_variable(&variable_name_size,
  1377. variable_name,
  1378. &vendor_guid);
  1379. switch (status) {
  1380. case EFI_SUCCESS:
  1381. efivar_create_sysfs_entry(efivars,
  1382. variable_name_size,
  1383. variable_name,
  1384. &vendor_guid);
  1385. break;
  1386. case EFI_NOT_FOUND:
  1387. break;
  1388. default:
  1389. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1390. status);
  1391. status = EFI_NOT_FOUND;
  1392. break;
  1393. }
  1394. } while (status != EFI_NOT_FOUND);
  1395. error = create_efivars_bin_attributes(efivars);
  1396. if (error)
  1397. unregister_efivars(efivars);
  1398. efivars->efi_pstore_info = efi_pstore_info;
  1399. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1400. if (efivars->efi_pstore_info.buf) {
  1401. efivars->efi_pstore_info.bufsize = 1024;
  1402. efivars->efi_pstore_info.data = efivars;
  1403. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1404. pstore_register(&efivars->efi_pstore_info);
  1405. }
  1406. register_filesystem(&efivarfs_type);
  1407. out:
  1408. kfree(variable_name);
  1409. return error;
  1410. }
  1411. EXPORT_SYMBOL_GPL(register_efivars);
  1412. /*
  1413. * For now we register the efi subsystem with the firmware subsystem
  1414. * and the vars subsystem with the efi subsystem. In the future, it
  1415. * might make sense to split off the efi subsystem into its own
  1416. * driver, but for now only efivars will register with it, so just
  1417. * include it here.
  1418. */
  1419. static int __init
  1420. efivars_init(void)
  1421. {
  1422. int error = 0;
  1423. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1424. EFIVARS_DATE);
  1425. if (!efi_enabled)
  1426. return 0;
  1427. /* For now we'll register the efi directory at /sys/firmware/efi */
  1428. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1429. if (!efi_kobj) {
  1430. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1431. return -ENOMEM;
  1432. }
  1433. ops.get_variable = efi.get_variable;
  1434. ops.set_variable = efi.set_variable;
  1435. ops.get_next_variable = efi.get_next_variable;
  1436. error = register_efivars(&__efivars, &ops, efi_kobj);
  1437. if (error)
  1438. goto err_put;
  1439. /* Don't forget the systab entry */
  1440. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1441. if (error) {
  1442. printk(KERN_ERR
  1443. "efivars: Sysfs attribute export failed with error %d.\n",
  1444. error);
  1445. goto err_unregister;
  1446. }
  1447. return 0;
  1448. err_unregister:
  1449. unregister_efivars(&__efivars);
  1450. err_put:
  1451. kobject_put(efi_kobj);
  1452. return error;
  1453. }
  1454. static void __exit
  1455. efivars_exit(void)
  1456. {
  1457. if (efi_enabled) {
  1458. unregister_efivars(&__efivars);
  1459. kobject_put(efi_kobj);
  1460. }
  1461. }
  1462. module_init(efivars_init);
  1463. module_exit(efivars_exit);