efivars.c 41 KB

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