efivars.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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/ctype.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. LIST_HEAD(efivar_sysfs_list);
  92. EXPORT_SYMBOL_GPL(efivar_sysfs_list);
  93. struct efivar_attribute {
  94. struct attribute attr;
  95. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  96. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  97. };
  98. /* Private pointer to registered efivars */
  99. static struct efivars *__efivars;
  100. static struct kset *efivars_kset;
  101. static struct bin_attribute *efivars_new_var;
  102. static struct bin_attribute *efivars_del_var;
  103. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  104. struct efivar_attribute efivar_attr_##_name = { \
  105. .attr = {.name = __stringify(_name), .mode = _mode}, \
  106. .show = _show, \
  107. .store = _store, \
  108. };
  109. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  110. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  111. /*
  112. * Prototype for sysfs creation function
  113. */
  114. static int
  115. efivar_create_sysfs_entry(struct efivar_entry *new_var);
  116. /*
  117. * Prototype for workqueue functions updating sysfs entry
  118. */
  119. static void efivar_update_sysfs_entries(struct work_struct *);
  120. static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
  121. static bool efivar_wq_enabled = true;
  122. static bool
  123. validate_device_path(struct efi_variable *var, int match, u8 *buffer,
  124. unsigned long len)
  125. {
  126. struct efi_generic_dev_path *node;
  127. int offset = 0;
  128. node = (struct efi_generic_dev_path *)buffer;
  129. if (len < sizeof(*node))
  130. return false;
  131. while (offset <= len - sizeof(*node) &&
  132. node->length >= sizeof(*node) &&
  133. node->length <= len - offset) {
  134. offset += node->length;
  135. if ((node->type == EFI_DEV_END_PATH ||
  136. node->type == EFI_DEV_END_PATH2) &&
  137. node->sub_type == EFI_DEV_END_ENTIRE)
  138. return true;
  139. node = (struct efi_generic_dev_path *)(buffer + offset);
  140. }
  141. /*
  142. * If we're here then either node->length pointed past the end
  143. * of the buffer or we reached the end of the buffer without
  144. * finding a device path end node.
  145. */
  146. return false;
  147. }
  148. static bool
  149. validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
  150. unsigned long len)
  151. {
  152. /* An array of 16-bit integers */
  153. if ((len % 2) != 0)
  154. return false;
  155. return true;
  156. }
  157. static bool
  158. validate_load_option(struct efi_variable *var, int match, u8 *buffer,
  159. unsigned long len)
  160. {
  161. u16 filepathlength;
  162. int i, desclength = 0, namelen;
  163. namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
  164. /* Either "Boot" or "Driver" followed by four digits of hex */
  165. for (i = match; i < match+4; i++) {
  166. if (var->VariableName[i] > 127 ||
  167. hex_to_bin(var->VariableName[i] & 0xff) < 0)
  168. return true;
  169. }
  170. /* Reject it if there's 4 digits of hex and then further content */
  171. if (namelen > match + 4)
  172. return false;
  173. /* A valid entry must be at least 8 bytes */
  174. if (len < 8)
  175. return false;
  176. filepathlength = buffer[4] | buffer[5] << 8;
  177. /*
  178. * There's no stored length for the description, so it has to be
  179. * found by hand
  180. */
  181. desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  182. /* Each boot entry must have a descriptor */
  183. if (!desclength)
  184. return false;
  185. /*
  186. * If the sum of the length of the description, the claimed filepath
  187. * length and the original header are greater than the length of the
  188. * variable, it's malformed
  189. */
  190. if ((desclength + filepathlength + 6) > len)
  191. return false;
  192. /*
  193. * And, finally, check the filepath
  194. */
  195. return validate_device_path(var, match, buffer + desclength + 6,
  196. filepathlength);
  197. }
  198. static bool
  199. validate_uint16(struct efi_variable *var, int match, u8 *buffer,
  200. unsigned long len)
  201. {
  202. /* A single 16-bit integer */
  203. if (len != 2)
  204. return false;
  205. return true;
  206. }
  207. static bool
  208. validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
  209. unsigned long len)
  210. {
  211. int i;
  212. for (i = 0; i < len; i++) {
  213. if (buffer[i] > 127)
  214. return false;
  215. if (buffer[i] == 0)
  216. return true;
  217. }
  218. return false;
  219. }
  220. struct variable_validate {
  221. char *name;
  222. bool (*validate)(struct efi_variable *var, int match, u8 *data,
  223. unsigned long len);
  224. };
  225. static const struct variable_validate variable_validate[] = {
  226. { "BootNext", validate_uint16 },
  227. { "BootOrder", validate_boot_order },
  228. { "DriverOrder", validate_boot_order },
  229. { "Boot*", validate_load_option },
  230. { "Driver*", validate_load_option },
  231. { "ConIn", validate_device_path },
  232. { "ConInDev", validate_device_path },
  233. { "ConOut", validate_device_path },
  234. { "ConOutDev", validate_device_path },
  235. { "ErrOut", validate_device_path },
  236. { "ErrOutDev", validate_device_path },
  237. { "Timeout", validate_uint16 },
  238. { "Lang", validate_ascii_string },
  239. { "PlatformLang", validate_ascii_string },
  240. { "", NULL },
  241. };
  242. bool
  243. efivar_validate(struct efi_variable *var, u8 *data, unsigned long len)
  244. {
  245. int i;
  246. u16 *unicode_name = var->VariableName;
  247. for (i = 0; variable_validate[i].validate != NULL; i++) {
  248. const char *name = variable_validate[i].name;
  249. int match;
  250. for (match = 0; ; match++) {
  251. char c = name[match];
  252. u16 u = unicode_name[match];
  253. /* All special variables are plain ascii */
  254. if (u > 127)
  255. return true;
  256. /* Wildcard in the matching name means we've matched */
  257. if (c == '*')
  258. return variable_validate[i].validate(var,
  259. match, data, len);
  260. /* Case sensitive match */
  261. if (c != u)
  262. break;
  263. /* Reached the end of the string while matching */
  264. if (!c)
  265. return variable_validate[i].validate(var,
  266. match, data, len);
  267. }
  268. }
  269. return true;
  270. }
  271. EXPORT_SYMBOL_GPL(efivar_validate);
  272. static efi_status_t
  273. check_var_size(u32 attributes, unsigned long size)
  274. {
  275. u64 storage_size, remaining_size, max_size;
  276. efi_status_t status;
  277. const struct efivar_operations *fops = __efivars->ops;
  278. if (!fops->query_variable_info)
  279. return EFI_UNSUPPORTED;
  280. status = fops->query_variable_info(attributes, &storage_size,
  281. &remaining_size, &max_size);
  282. if (status != EFI_SUCCESS)
  283. return status;
  284. if (!storage_size || size > remaining_size || size > max_size ||
  285. (remaining_size - size) < (storage_size / 2))
  286. return EFI_OUT_OF_RESOURCES;
  287. return status;
  288. }
  289. static ssize_t
  290. efivar_guid_read(struct efivar_entry *entry, char *buf)
  291. {
  292. struct efi_variable *var = &entry->var;
  293. char *str = buf;
  294. if (!entry || !buf)
  295. return 0;
  296. efi_guid_unparse(&var->VendorGuid, str);
  297. str += strlen(str);
  298. str += sprintf(str, "\n");
  299. return str - buf;
  300. }
  301. static ssize_t
  302. efivar_attr_read(struct efivar_entry *entry, char *buf)
  303. {
  304. struct efi_variable *var = &entry->var;
  305. char *str = buf;
  306. if (!entry || !buf)
  307. return -EINVAL;
  308. var->DataSize = 1024;
  309. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  310. return -EIO;
  311. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  312. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  313. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  314. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  315. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  316. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  317. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  318. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  319. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  320. str += sprintf(str,
  321. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  322. if (var->Attributes &
  323. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  324. str += sprintf(str,
  325. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  326. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  327. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  328. return str - buf;
  329. }
  330. static ssize_t
  331. efivar_size_read(struct efivar_entry *entry, char *buf)
  332. {
  333. struct efi_variable *var = &entry->var;
  334. char *str = buf;
  335. if (!entry || !buf)
  336. return -EINVAL;
  337. var->DataSize = 1024;
  338. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  339. return -EIO;
  340. str += sprintf(str, "0x%lx\n", var->DataSize);
  341. return str - buf;
  342. }
  343. static ssize_t
  344. efivar_data_read(struct efivar_entry *entry, char *buf)
  345. {
  346. struct efi_variable *var = &entry->var;
  347. if (!entry || !buf)
  348. return -EINVAL;
  349. var->DataSize = 1024;
  350. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  351. return -EIO;
  352. memcpy(buf, var->Data, var->DataSize);
  353. return var->DataSize;
  354. }
  355. /*
  356. * We allow each variable to be edited via rewriting the
  357. * entire efi variable structure.
  358. */
  359. static ssize_t
  360. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  361. {
  362. struct efi_variable *new_var, *var = &entry->var;
  363. int err;
  364. if (count != sizeof(struct efi_variable))
  365. return -EINVAL;
  366. new_var = (struct efi_variable *)buf;
  367. /*
  368. * If only updating the variable data, then the name
  369. * and guid should remain the same
  370. */
  371. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  372. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  373. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  374. return -EINVAL;
  375. }
  376. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  377. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  378. return -EINVAL;
  379. }
  380. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  381. efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) {
  382. printk(KERN_ERR "efivars: Malformed variable content\n");
  383. return -EINVAL;
  384. }
  385. memcpy(&entry->var, new_var, count);
  386. err = efivar_entry_set(entry, new_var->Attributes,
  387. new_var->DataSize, new_var->Data, false);
  388. if (err) {
  389. printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
  390. return -EIO;
  391. }
  392. return count;
  393. }
  394. static ssize_t
  395. efivar_show_raw(struct efivar_entry *entry, char *buf)
  396. {
  397. struct efi_variable *var = &entry->var;
  398. if (!entry || !buf)
  399. return 0;
  400. var->DataSize = 1024;
  401. if (efivar_entry_get(entry, &entry->var.Attributes,
  402. &entry->var.DataSize, entry->var.Data))
  403. return -EIO;
  404. memcpy(buf, var, sizeof(*var));
  405. return sizeof(*var);
  406. }
  407. /*
  408. * Generic read/write functions that call the specific functions of
  409. * the attributes...
  410. */
  411. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  412. char *buf)
  413. {
  414. struct efivar_entry *var = to_efivar_entry(kobj);
  415. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  416. ssize_t ret = -EIO;
  417. if (!capable(CAP_SYS_ADMIN))
  418. return -EACCES;
  419. if (efivar_attr->show) {
  420. ret = efivar_attr->show(var, buf);
  421. }
  422. return ret;
  423. }
  424. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  425. const char *buf, size_t count)
  426. {
  427. struct efivar_entry *var = to_efivar_entry(kobj);
  428. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  429. ssize_t ret = -EIO;
  430. if (!capable(CAP_SYS_ADMIN))
  431. return -EACCES;
  432. if (efivar_attr->store)
  433. ret = efivar_attr->store(var, buf, count);
  434. return ret;
  435. }
  436. static const struct sysfs_ops efivar_attr_ops = {
  437. .show = efivar_attr_show,
  438. .store = efivar_attr_store,
  439. };
  440. static void efivar_release(struct kobject *kobj)
  441. {
  442. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  443. kfree(var);
  444. }
  445. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  446. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  447. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  448. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  449. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  450. static struct attribute *def_attrs[] = {
  451. &efivar_attr_guid.attr,
  452. &efivar_attr_size.attr,
  453. &efivar_attr_attributes.attr,
  454. &efivar_attr_data.attr,
  455. &efivar_attr_raw_var.attr,
  456. NULL,
  457. };
  458. static struct kobj_type efivar_ktype = {
  459. .release = efivar_release,
  460. .sysfs_ops = &efivar_attr_ops,
  461. .default_attrs = def_attrs,
  462. };
  463. static int efi_status_to_err(efi_status_t status)
  464. {
  465. int err;
  466. switch (status) {
  467. case EFI_SUCCESS:
  468. err = 0;
  469. break;
  470. case EFI_INVALID_PARAMETER:
  471. err = -EINVAL;
  472. break;
  473. case EFI_OUT_OF_RESOURCES:
  474. err = -ENOSPC;
  475. break;
  476. case EFI_DEVICE_ERROR:
  477. err = -EIO;
  478. break;
  479. case EFI_WRITE_PROTECTED:
  480. err = -EROFS;
  481. break;
  482. case EFI_SECURITY_VIOLATION:
  483. err = -EACCES;
  484. break;
  485. case EFI_NOT_FOUND:
  486. err = -ENOENT;
  487. break;
  488. default:
  489. err = -EINVAL;
  490. }
  491. return err;
  492. }
  493. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  494. struct bin_attribute *bin_attr,
  495. char *buf, loff_t pos, size_t count)
  496. {
  497. struct efi_variable *new_var = (struct efi_variable *)buf;
  498. struct efivar_entry *new_entry;
  499. int err;
  500. if (!capable(CAP_SYS_ADMIN))
  501. return -EACCES;
  502. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  503. efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) {
  504. printk(KERN_ERR "efivars: Malformed variable content\n");
  505. return -EINVAL;
  506. }
  507. new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
  508. if (!new_entry)
  509. return -ENOMEM;
  510. memcpy(&new_entry->var, new_var, sizeof(*new_var));
  511. err = efivar_entry_set(new_entry, new_var->Attributes, new_var->DataSize,
  512. new_var->Data, &efivar_sysfs_list);
  513. if (err) {
  514. if (err == -EEXIST)
  515. err = -EINVAL;
  516. goto out;
  517. }
  518. if (efivar_create_sysfs_entry(new_entry)) {
  519. printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
  520. kfree(new_entry);
  521. }
  522. return count;
  523. out:
  524. kfree(new_entry);
  525. return err;
  526. }
  527. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  528. struct bin_attribute *bin_attr,
  529. char *buf, loff_t pos, size_t count)
  530. {
  531. struct efi_variable *del_var = (struct efi_variable *)buf;
  532. struct efivar_entry *entry;
  533. int err = 0;
  534. if (!capable(CAP_SYS_ADMIN))
  535. return -EACCES;
  536. efivar_entry_iter_begin();
  537. entry = efivar_entry_find(del_var->VariableName, del_var->VendorGuid,
  538. &efivar_sysfs_list, true);
  539. if (!entry)
  540. err = -EINVAL;
  541. else if (__efivar_entry_delete(entry))
  542. err = -EIO;
  543. efivar_entry_iter_end();
  544. if (err)
  545. return err;
  546. efivar_unregister(entry);
  547. /* It's dead Jim.... */
  548. return count;
  549. }
  550. static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
  551. struct list_head *head)
  552. {
  553. struct efivar_entry *entry, *n;
  554. unsigned long strsize1, strsize2;
  555. bool found = false;
  556. strsize1 = utf16_strsize(variable_name, 1024);
  557. list_for_each_entry_safe(entry, n, head, list) {
  558. strsize2 = utf16_strsize(entry->var.VariableName, 1024);
  559. if (strsize1 == strsize2 &&
  560. !memcmp(variable_name, &(entry->var.VariableName),
  561. strsize2) &&
  562. !efi_guidcmp(entry->var.VendorGuid,
  563. *vendor)) {
  564. found = true;
  565. break;
  566. }
  567. }
  568. return found;
  569. }
  570. static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
  571. unsigned long name_size, void *data)
  572. {
  573. struct efivar_entry *entry = data;
  574. if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
  575. return 0;
  576. memcpy(entry->var.VariableName, name, name_size);
  577. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  578. return 1;
  579. }
  580. /*
  581. * Returns the size of variable_name, in bytes, including the
  582. * terminating NULL character, or variable_name_size if no NULL
  583. * character is found among the first variable_name_size bytes.
  584. */
  585. static unsigned long var_name_strnsize(efi_char16_t *variable_name,
  586. unsigned long variable_name_size)
  587. {
  588. unsigned long len;
  589. efi_char16_t c;
  590. /*
  591. * The variable name is, by definition, a NULL-terminated
  592. * string, so make absolutely sure that variable_name_size is
  593. * the value we expect it to be. If not, return the real size.
  594. */
  595. for (len = 2; len <= variable_name_size; len += sizeof(c)) {
  596. c = variable_name[(len / sizeof(c)) - 1];
  597. if (!c)
  598. break;
  599. }
  600. return min(len, variable_name_size);
  601. }
  602. static void efivar_update_sysfs_entries(struct work_struct *work)
  603. {
  604. struct efivar_entry *entry;
  605. int err;
  606. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  607. if (!entry)
  608. return;
  609. /* Add new sysfs entries */
  610. while (1) {
  611. memset(entry, 0, sizeof(*entry));
  612. err = efivar_init(efivar_update_sysfs_entry, entry,
  613. true, false, &efivar_sysfs_list);
  614. if (!err)
  615. break;
  616. efivar_create_sysfs_entry(entry);
  617. }
  618. kfree(entry);
  619. }
  620. /*
  621. * Let's not leave out systab information that snuck into
  622. * the efivars driver
  623. */
  624. static ssize_t systab_show(struct kobject *kobj,
  625. struct kobj_attribute *attr, char *buf)
  626. {
  627. char *str = buf;
  628. if (!kobj || !buf)
  629. return -EINVAL;
  630. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  631. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  632. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  633. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  634. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  635. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  636. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  637. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  638. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  639. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  640. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  641. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  642. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  643. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  644. return str - buf;
  645. }
  646. static struct kobj_attribute efi_attr_systab =
  647. __ATTR(systab, 0400, systab_show, NULL);
  648. static struct attribute *efi_subsys_attrs[] = {
  649. &efi_attr_systab.attr,
  650. NULL, /* maybe more in the future? */
  651. };
  652. static struct attribute_group efi_subsys_attr_group = {
  653. .attrs = efi_subsys_attrs,
  654. };
  655. static struct kobject *efi_kobj;
  656. /**
  657. * efivar_create_sysfs_entry - create a new entry in sysfs
  658. * @new_var: efivar entry to create
  659. *
  660. * Returns 1 on failure, 0 on success
  661. */
  662. static int
  663. efivar_create_sysfs_entry(struct efivar_entry *new_var)
  664. {
  665. int i, short_name_size;
  666. char *short_name;
  667. unsigned long variable_name_size;
  668. efi_char16_t *variable_name;
  669. variable_name = new_var->var.VariableName;
  670. variable_name_size = utf16_strlen(variable_name) * sizeof(efi_char16_t);
  671. /*
  672. * Length of the variable bytes in ASCII, plus the '-' separator,
  673. * plus the GUID, plus trailing NUL
  674. */
  675. short_name_size = variable_name_size / sizeof(efi_char16_t)
  676. + 1 + EFI_VARIABLE_GUID_LEN + 1;
  677. short_name = kzalloc(short_name_size, GFP_KERNEL);
  678. if (!short_name) {
  679. kfree(short_name);
  680. return 1;
  681. }
  682. /* Convert Unicode to normal chars (assume top bits are 0),
  683. ala UTF-8 */
  684. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  685. short_name[i] = variable_name[i] & 0xFF;
  686. }
  687. /* This is ugly, but necessary to separate one vendor's
  688. private variables from another's. */
  689. *(short_name + strlen(short_name)) = '-';
  690. efi_guid_unparse(&new_var->var.VendorGuid,
  691. short_name + strlen(short_name));
  692. new_var->kobj.kset = efivars_kset;
  693. i = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
  694. NULL, "%s", short_name);
  695. kfree(short_name);
  696. if (i)
  697. return 1;
  698. kobject_uevent(&new_var->kobj, KOBJ_ADD);
  699. efivar_entry_add(new_var, &efivar_sysfs_list);
  700. return 0;
  701. }
  702. static int
  703. create_efivars_bin_attributes(void)
  704. {
  705. struct bin_attribute *attr;
  706. int error;
  707. /* new_var */
  708. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  709. if (!attr)
  710. return -ENOMEM;
  711. attr->attr.name = "new_var";
  712. attr->attr.mode = 0200;
  713. attr->write = efivar_create;
  714. efivars_new_var = attr;
  715. /* del_var */
  716. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  717. if (!attr) {
  718. error = -ENOMEM;
  719. goto out_free;
  720. }
  721. attr->attr.name = "del_var";
  722. attr->attr.mode = 0200;
  723. attr->write = efivar_delete;
  724. efivars_del_var = attr;
  725. sysfs_bin_attr_init(efivars_new_var);
  726. sysfs_bin_attr_init(efivars_del_var);
  727. /* Register */
  728. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
  729. if (error) {
  730. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  731. " due to error %d\n", error);
  732. goto out_free;
  733. }
  734. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
  735. if (error) {
  736. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  737. " due to error %d\n", error);
  738. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  739. goto out_free;
  740. }
  741. return 0;
  742. out_free:
  743. kfree(efivars_del_var);
  744. efivars_del_var = NULL;
  745. kfree(efivars_new_var);
  746. efivars_new_var = NULL;
  747. return error;
  748. }
  749. static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
  750. unsigned long name_size, void *data)
  751. {
  752. struct efivar_entry *entry;
  753. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  754. if (!entry)
  755. return -ENOMEM;
  756. memcpy(entry->var.VariableName, name, name_size);
  757. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  758. efivar_create_sysfs_entry(entry);
  759. return 0;
  760. }
  761. static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
  762. {
  763. efivar_entry_remove(entry);
  764. efivar_unregister(entry);
  765. return 0;
  766. }
  767. /*
  768. * Print a warning when duplicate EFI variables are encountered and
  769. * disable the sysfs workqueue since the firmware is buggy.
  770. */
  771. static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
  772. unsigned long len16)
  773. {
  774. size_t i, len8 = len16 / sizeof(efi_char16_t);
  775. char *s8;
  776. /*
  777. * Disable the workqueue since the algorithm it uses for
  778. * detecting new variables won't work with this buggy
  779. * implementation of GetNextVariableName().
  780. */
  781. efivar_wq_enabled = false;
  782. s8 = kzalloc(len8, GFP_KERNEL);
  783. if (!s8)
  784. return;
  785. for (i = 0; i < len8; i++)
  786. s8[i] = s16[i];
  787. printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
  788. s8, vendor_guid);
  789. kfree(s8);
  790. }
  791. static struct kobject *efivars_kobj;
  792. void efivars_sysfs_exit(void)
  793. {
  794. /* Remove all entries and destroy */
  795. __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL, NULL);
  796. if (efivars_new_var)
  797. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  798. if (efivars_del_var)
  799. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
  800. kfree(efivars_new_var);
  801. kfree(efivars_del_var);
  802. kobject_put(efivars_kobj);
  803. kset_unregister(efivars_kset);
  804. }
  805. int efivars_sysfs_init(void)
  806. {
  807. struct kobject *parent_kobj = efivars_kobject();
  808. int error = 0;
  809. /* No efivars has been registered yet */
  810. if (!parent_kobj)
  811. return 0;
  812. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  813. EFIVARS_DATE);
  814. efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
  815. if (!efivars_kset) {
  816. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  817. return -ENOMEM;
  818. }
  819. efivars_kobj = kobject_create_and_add("efivars", parent_kobj);
  820. if (!efivars_kobj) {
  821. pr_err("efivars: Subsystem registration failed.\n");
  822. kset_unregister(efivars_kset);
  823. return -ENOMEM;
  824. }
  825. efivar_init(efivars_sysfs_callback, NULL, false,
  826. true, &efivar_sysfs_list);
  827. error = create_efivars_bin_attributes();
  828. if (error)
  829. efivars_sysfs_exit();
  830. return error;
  831. }
  832. EXPORT_SYMBOL_GPL(efivars_sysfs_init);
  833. /**
  834. * efivar_init - build the initial list of EFI variables
  835. * @func: callback function to invoke for every variable
  836. * @data: function-specific data to pass to @func
  837. * @atomic: do we need to execute the @func-loop atomically?
  838. * @duplicates: error if we encounter duplicates on @head?
  839. * @head: initialised head of variable list
  840. *
  841. * Get every EFI variable from the firmware and invoke @func. @func
  842. * should call efivar_entry_add() to build the list of variables.
  843. *
  844. * Returns 0 on success, or a kernel error code on failure.
  845. */
  846. int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
  847. void *data, bool atomic, bool duplicates,
  848. struct list_head *head)
  849. {
  850. const struct efivar_operations *ops = __efivars->ops;
  851. unsigned long variable_name_size = 1024;
  852. efi_char16_t *variable_name;
  853. efi_status_t status;
  854. efi_guid_t vendor_guid;
  855. int err = 0;
  856. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  857. if (!variable_name) {
  858. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  859. return -ENOMEM;
  860. }
  861. spin_lock_irq(&__efivars->lock);
  862. /*
  863. * Per EFI spec, the maximum storage allocated for both
  864. * the variable name and variable data is 1024 bytes.
  865. */
  866. do {
  867. variable_name_size = 1024;
  868. status = ops->get_next_variable(&variable_name_size,
  869. variable_name,
  870. &vendor_guid);
  871. switch (status) {
  872. case EFI_SUCCESS:
  873. if (!atomic)
  874. spin_unlock_irq(&__efivars->lock);
  875. variable_name_size = var_name_strnsize(variable_name,
  876. variable_name_size);
  877. /*
  878. * Some firmware implementations return the
  879. * same variable name on multiple calls to
  880. * get_next_variable(). Terminate the loop
  881. * immediately as there is no guarantee that
  882. * we'll ever see a different variable name,
  883. * and may end up looping here forever.
  884. */
  885. if (duplicates &&
  886. variable_is_present(variable_name, &vendor_guid, head)) {
  887. dup_variable_bug(variable_name, &vendor_guid,
  888. variable_name_size);
  889. if (!atomic)
  890. spin_lock_irq(&__efivars->lock);
  891. status = EFI_NOT_FOUND;
  892. break;
  893. }
  894. err = func(variable_name, vendor_guid, variable_name_size, data);
  895. if (err)
  896. status = EFI_NOT_FOUND;
  897. if (!atomic)
  898. spin_lock_irq(&__efivars->lock);
  899. break;
  900. case EFI_NOT_FOUND:
  901. break;
  902. default:
  903. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  904. status);
  905. status = EFI_NOT_FOUND;
  906. break;
  907. }
  908. } while (status != EFI_NOT_FOUND);
  909. spin_unlock_irq(&__efivars->lock);
  910. kfree(variable_name);
  911. return err;
  912. }
  913. EXPORT_SYMBOL_GPL(efivar_init);
  914. /**
  915. * efivar_entry_add - add entry to variable list
  916. * @entry: entry to add to list
  917. * @head: list head
  918. */
  919. void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
  920. {
  921. spin_lock_irq(&__efivars->lock);
  922. list_add(&entry->list, head);
  923. spin_unlock_irq(&__efivars->lock);
  924. }
  925. EXPORT_SYMBOL_GPL(efivar_entry_add);
  926. /**
  927. * efivar_entry_remove - remove entry from variable list
  928. * @entry: entry to remove from list
  929. */
  930. void efivar_entry_remove(struct efivar_entry *entry)
  931. {
  932. spin_lock_irq(&__efivars->lock);
  933. list_del(&entry->list);
  934. spin_unlock_irq(&__efivars->lock);
  935. }
  936. EXPORT_SYMBOL_GPL(efivar_entry_remove);
  937. /*
  938. * efivar_entry_list_del_unlock - remove entry from variable list
  939. * @entry: entry to remove
  940. *
  941. * Remove @entry from the variable list and release the list lock.
  942. *
  943. * NOTE: slightly weird locking semantics here - we expect to be
  944. * called with the efivars lock already held, and we release it before
  945. * returning. This is because this function is usually called after
  946. * set_variable() while the lock is still held.
  947. */
  948. static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
  949. {
  950. WARN_ON(!spin_is_locked(&__efivars->lock));
  951. list_del(&entry->list);
  952. spin_unlock_irq(&__efivars->lock);
  953. }
  954. /**
  955. * __efivar_entry_delete - delete an EFI variable
  956. * @entry: entry containing EFI variable to delete
  957. *
  958. * Delete the variable from the firmware and remove @entry from the
  959. * variable list. It is the caller's responsibility to free @entry
  960. * once we return.
  961. *
  962. * This function differs from efivar_entry_delete() because it is
  963. * safe to be called from within a efivar_entry_iter_begin() and
  964. * efivar_entry_iter_end() region, unlike efivar_entry_delete().
  965. *
  966. * Returns 0 on success, or a converted EFI status code if
  967. * set_variable() fails. If set_variable() fails the entry remains
  968. * on the list.
  969. */
  970. int __efivar_entry_delete(struct efivar_entry *entry)
  971. {
  972. const struct efivar_operations *ops = __efivars->ops;
  973. efi_status_t status;
  974. WARN_ON(!spin_is_locked(&__efivars->lock));
  975. status = ops->set_variable(entry->var.VariableName,
  976. &entry->var.VendorGuid,
  977. 0, 0, NULL);
  978. if (status)
  979. return efi_status_to_err(status);
  980. list_del(&entry->list);
  981. return 0;
  982. }
  983. EXPORT_SYMBOL_GPL(__efivar_entry_delete);
  984. /**
  985. * efivar_entry_delete - delete variable and remove entry from list
  986. * @entry: entry containing variable to delete
  987. *
  988. * Delete the variable from the firmware and remove @entry from the
  989. * variable list. It is the caller's responsibility to free @entry
  990. * once we return.
  991. *
  992. * Returns 0 on success, or a converted EFI status code if
  993. * set_variable() fails.
  994. */
  995. int efivar_entry_delete(struct efivar_entry *entry)
  996. {
  997. const struct efivar_operations *ops = __efivars->ops;
  998. efi_status_t status;
  999. spin_lock_irq(&__efivars->lock);
  1000. status = ops->set_variable(entry->var.VariableName,
  1001. &entry->var.VendorGuid,
  1002. 0, 0, NULL);
  1003. if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
  1004. spin_unlock_irq(&__efivars->lock);
  1005. return efi_status_to_err(status);
  1006. }
  1007. efivar_entry_list_del_unlock(entry);
  1008. return 0;
  1009. }
  1010. EXPORT_SYMBOL_GPL(efivar_entry_delete);
  1011. /**
  1012. * efivar_entry_set - call set_variable()
  1013. * @entry: entry containing the EFI variable to write
  1014. * @attributes: variable attributes
  1015. * @size: size of @data buffer
  1016. * @data: buffer containing variable data
  1017. * @head: head of variable list
  1018. *
  1019. * Calls set_variable() for an EFI variable. If creating a new EFI
  1020. * variable, this function is usually followed by efivar_entry_add().
  1021. *
  1022. * Before writing the variable, the remaining EFI variable storage
  1023. * space is checked to ensure there is enough room available.
  1024. *
  1025. * If @head is not NULL a lookup is performed to determine whether
  1026. * the entry is already on the list.
  1027. *
  1028. * Returns 0 on success, -EEXIST if a lookup is performed and the entry
  1029. * already exists on the list, or a converted EFI status code if
  1030. * set_variable() fails.
  1031. */
  1032. int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
  1033. unsigned long size, void *data, struct list_head *head)
  1034. {
  1035. const struct efivar_operations *ops = __efivars->ops;
  1036. efi_status_t status;
  1037. efi_char16_t *name = entry->var.VariableName;
  1038. efi_guid_t vendor = entry->var.VendorGuid;
  1039. spin_lock_irq(&__efivars->lock);
  1040. if (head && efivar_entry_find(name, vendor, head, false)) {
  1041. spin_unlock_irq(&__efivars->lock);
  1042. return -EEXIST;
  1043. }
  1044. status = check_var_size(attributes, size + utf16_strsize(name, 1024));
  1045. if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
  1046. status = ops->set_variable(name, &vendor,
  1047. attributes, size, data);
  1048. spin_unlock_irq(&__efivars->lock);
  1049. return efi_status_to_err(status);
  1050. }
  1051. EXPORT_SYMBOL_GPL(efivar_entry_set);
  1052. /**
  1053. * efivar_entry_set_safe - call set_variable() if enough space in firmware
  1054. * @name: buffer containing the variable name
  1055. * @vendor: variable vendor guid
  1056. * @attributes: variable attributes
  1057. * @block: can we block in this context?
  1058. * @size: size of @data buffer
  1059. * @data: buffer containing variable data
  1060. *
  1061. * Ensures there is enough free storage in the firmware for this variable, and
  1062. * if so, calls set_variable(). If creating a new EFI variable, this function
  1063. * is usually followed by efivar_entry_add().
  1064. *
  1065. * Returns 0 on success, -ENOSPC if the firmware does not have enough
  1066. * space for set_variable() to succeed, or a converted EFI status code
  1067. * if set_variable() fails.
  1068. */
  1069. int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
  1070. bool block, unsigned long size, void *data)
  1071. {
  1072. const struct efivar_operations *ops = __efivars->ops;
  1073. unsigned long flags;
  1074. efi_status_t status;
  1075. if (!ops->query_variable_info)
  1076. return -ENOSYS;
  1077. if (!block && !spin_trylock_irqsave(&__efivars->lock, flags))
  1078. return -EBUSY;
  1079. else
  1080. spin_lock_irqsave(&__efivars->lock, flags);
  1081. status = check_var_size(attributes, size + utf16_strsize(name, 1024));
  1082. if (status != EFI_SUCCESS) {
  1083. spin_unlock_irqrestore(&__efivars->lock, flags);
  1084. return -ENOSPC;
  1085. }
  1086. status = ops->set_variable(name, &vendor, attributes, size, data);
  1087. spin_unlock_irqrestore(&__efivars->lock, flags);
  1088. return efi_status_to_err(status);
  1089. }
  1090. EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
  1091. /**
  1092. * efivar_entry_find - search for an entry
  1093. * @name: the EFI variable name
  1094. * @guid: the EFI variable vendor's guid
  1095. * @head: head of the variable list
  1096. * @remove: should we remove the entry from the list?
  1097. *
  1098. * Search for an entry on the variable list that has the EFI variable
  1099. * name @name and vendor guid @guid. If an entry is found on the list
  1100. * and @remove is true, the entry is removed from the list.
  1101. *
  1102. * The caller MUST call efivar_entry_iter_begin() and
  1103. * efivar_entry_iter_end() before and after the invocation of this
  1104. * function, respectively.
  1105. *
  1106. * Returns the entry if found on the list, %NULL otherwise.
  1107. */
  1108. struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
  1109. struct list_head *head, bool remove)
  1110. {
  1111. struct efivar_entry *entry, *n;
  1112. int strsize1, strsize2;
  1113. bool found = false;
  1114. WARN_ON(!spin_is_locked(&__efivars->lock));
  1115. list_for_each_entry_safe(entry, n, head, list) {
  1116. strsize1 = utf16_strsize(name, 1024);
  1117. strsize2 = utf16_strsize(entry->var.VariableName, 1024);
  1118. if (strsize1 == strsize2 &&
  1119. !memcmp(name, &(entry->var.VariableName), strsize1) &&
  1120. !efi_guidcmp(guid, entry->var.VendorGuid)) {
  1121. found = true;
  1122. break;
  1123. }
  1124. }
  1125. if (!found)
  1126. return NULL;
  1127. if (remove)
  1128. list_del(&entry->list);
  1129. return entry;
  1130. }
  1131. EXPORT_SYMBOL_GPL(efivar_entry_find);
  1132. /**
  1133. * __efivar_entry_size - obtain the size of a variable
  1134. * @entry: entry for this variable
  1135. * @size: location to store the variable's size
  1136. *
  1137. * The caller MUST call efivar_entry_iter_begin() and
  1138. * efivar_entry_iter_end() before and after the invocation of this
  1139. * function, respectively.
  1140. */
  1141. int __efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
  1142. {
  1143. const struct efivar_operations *ops = __efivars->ops;
  1144. efi_status_t status;
  1145. WARN_ON(!spin_is_locked(&__efivars->lock));
  1146. *size = 0;
  1147. status = ops->get_variable(entry->var.VariableName,
  1148. &entry->var.VendorGuid, NULL, size, NULL);
  1149. if (status != EFI_BUFFER_TOO_SMALL)
  1150. return efi_status_to_err(status);
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL_GPL(__efivar_entry_size);
  1154. /**
  1155. * efivar_entry_size - obtain the size of a variable
  1156. * @entry: entry for this variable
  1157. * @size: location to store the variable's size
  1158. */
  1159. int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
  1160. {
  1161. const struct efivar_operations *ops = __efivars->ops;
  1162. efi_status_t status;
  1163. *size = 0;
  1164. spin_lock_irq(&__efivars->lock);
  1165. status = ops->get_variable(entry->var.VariableName,
  1166. &entry->var.VendorGuid, NULL, size, NULL);
  1167. spin_unlock_irq(&__efivars->lock);
  1168. if (status != EFI_BUFFER_TOO_SMALL)
  1169. return efi_status_to_err(status);
  1170. return 0;
  1171. }
  1172. EXPORT_SYMBOL_GPL(efivar_entry_size);
  1173. /**
  1174. * efivar_entry_get - call get_variable()
  1175. * @entry: read data for this variable
  1176. * @attributes: variable attributes
  1177. * @size: size of @data buffer
  1178. * @data: buffer to store variable data
  1179. */
  1180. int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  1181. unsigned long *size, void *data)
  1182. {
  1183. const struct efivar_operations *ops = __efivars->ops;
  1184. efi_status_t status;
  1185. spin_lock_irq(&__efivars->lock);
  1186. status = ops->get_variable(entry->var.VariableName,
  1187. &entry->var.VendorGuid,
  1188. attributes, size, data);
  1189. spin_unlock_irq(&__efivars->lock);
  1190. return efi_status_to_err(status);
  1191. }
  1192. EXPORT_SYMBOL_GPL(efivar_entry_get);
  1193. /**
  1194. * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
  1195. * @entry: entry containing variable to set and get
  1196. * @attributes: attributes of variable to be written
  1197. * @size: size of data buffer
  1198. * @data: buffer containing data to write
  1199. * @set: did the set_variable() call succeed?
  1200. *
  1201. * This is a pretty special (complex) function. See efivarfs_file_write().
  1202. *
  1203. * Atomically call set_variable() for @entry and if the call is
  1204. * successful, return the new size of the variable from get_variable()
  1205. * in @size. The success of set_variable() is indicated by @set.
  1206. *
  1207. * Returns 0 on success, -EINVAL if the variable data is invalid,
  1208. * -ENOSPC if the firmware does not have enough available space, or a
  1209. * converted EFI status code if either of set_variable() or
  1210. * get_variable() fail.
  1211. *
  1212. * If the EFI variable does not exist when calling set_variable()
  1213. * (EFI_NOT_FOUND), @entry is removed from the variable list.
  1214. */
  1215. int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
  1216. unsigned long *size, void *data, bool *set)
  1217. {
  1218. const struct efivar_operations *ops = __efivars->ops;
  1219. efi_char16_t *name = entry->var.VariableName;
  1220. efi_guid_t *vendor = &entry->var.VendorGuid;
  1221. efi_status_t status;
  1222. int err;
  1223. *set = false;
  1224. if (efivar_validate(&entry->var, data, *size) == false)
  1225. return -EINVAL;
  1226. /*
  1227. * The lock here protects the get_variable call, the conditional
  1228. * set_variable call, and removal of the variable from the efivars
  1229. * list (in the case of an authenticated delete).
  1230. */
  1231. spin_lock_irq(&__efivars->lock);
  1232. /*
  1233. * Ensure that the available space hasn't shrunk below the safe level
  1234. */
  1235. status = check_var_size(attributes, *size + utf16_strsize(name, 1024));
  1236. if (status != EFI_SUCCESS) {
  1237. if (status != EFI_UNSUPPORTED) {
  1238. err = efi_status_to_err(status);
  1239. goto out;
  1240. }
  1241. if (*size > 65536) {
  1242. err = -ENOSPC;
  1243. goto out;
  1244. }
  1245. }
  1246. status = ops->set_variable(name, vendor, attributes, *size, data);
  1247. if (status != EFI_SUCCESS) {
  1248. err = efi_status_to_err(status);
  1249. goto out;
  1250. }
  1251. *set = true;
  1252. /*
  1253. * Writing to the variable may have caused a change in size (which
  1254. * could either be an append or an overwrite), or the variable to be
  1255. * deleted. Perform a GetVariable() so we can tell what actually
  1256. * happened.
  1257. */
  1258. *size = 0;
  1259. status = ops->get_variable(entry->var.VariableName,
  1260. &entry->var.VendorGuid,
  1261. NULL, size, NULL);
  1262. if (status == EFI_NOT_FOUND)
  1263. efivar_entry_list_del_unlock(entry);
  1264. else
  1265. spin_unlock_irq(&__efivars->lock);
  1266. if (status && status != EFI_BUFFER_TOO_SMALL)
  1267. return efi_status_to_err(status);
  1268. return 0;
  1269. out:
  1270. spin_unlock_irq(&__efivars->lock);
  1271. return err;
  1272. }
  1273. EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
  1274. /**
  1275. * efivar_entry_iter_begin - begin iterating the variable list
  1276. *
  1277. * Lock the variable list to prevent entry insertion and removal until
  1278. * efivar_entry_iter_end() is called. This function is usually used in
  1279. * conjunction with __efivar_entry_iter() or efivar_entry_iter().
  1280. */
  1281. void efivar_entry_iter_begin(void)
  1282. {
  1283. spin_lock_irq(&__efivars->lock);
  1284. }
  1285. EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
  1286. /**
  1287. * efivar_entry_iter_end - finish iterating the variable list
  1288. *
  1289. * Unlock the variable list and allow modifications to the list again.
  1290. */
  1291. void efivar_entry_iter_end(void)
  1292. {
  1293. spin_unlock_irq(&__efivars->lock);
  1294. }
  1295. EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
  1296. /**
  1297. * __efivar_entry_iter - iterate over variable list
  1298. * @func: callback function
  1299. * @head: head of the variable list
  1300. * @data: function-specific data to pass to callback
  1301. * @prev: entry to begin iterating from
  1302. *
  1303. * Iterate over the list of EFI variables and call @func with every
  1304. * entry on the list. It is safe for @func to remove entries in the
  1305. * list via efivar_entry_delete().
  1306. *
  1307. * You MUST call efivar_enter_iter_begin() before this function, and
  1308. * efivar_entry_iter_end() afterwards.
  1309. *
  1310. * It is possible to begin iteration from an arbitrary entry within
  1311. * the list by passing @prev. @prev is updated on return to point to
  1312. * the last entry passed to @func. To begin iterating from the
  1313. * beginning of the list @prev must be %NULL.
  1314. *
  1315. * The restrictions for @func are the same as documented for
  1316. * efivar_entry_iter().
  1317. */
  1318. int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  1319. struct list_head *head, void *data,
  1320. struct efivar_entry **prev)
  1321. {
  1322. struct efivar_entry *entry, *n;
  1323. int err = 0;
  1324. if (!prev || !*prev) {
  1325. list_for_each_entry_safe(entry, n, head, list) {
  1326. err = func(entry, data);
  1327. if (err)
  1328. break;
  1329. }
  1330. if (prev)
  1331. *prev = entry;
  1332. return err;
  1333. }
  1334. list_for_each_entry_safe_continue((*prev), n, head, list) {
  1335. err = func(*prev, data);
  1336. if (err)
  1337. break;
  1338. }
  1339. return err;
  1340. }
  1341. EXPORT_SYMBOL_GPL(__efivar_entry_iter);
  1342. /**
  1343. * efivar_entry_iter - iterate over variable list
  1344. * @func: callback function
  1345. * @head: head of variable list
  1346. * @data: function-specific data to pass to callback
  1347. *
  1348. * Iterate over the list of EFI variables and call @func with every
  1349. * entry on the list. It is safe for @func to remove entries in the
  1350. * list via efivar_entry_delete() while iterating.
  1351. *
  1352. * Some notes for the callback function:
  1353. * - a non-zero return value indicates an error and terminates the loop
  1354. * - @func is called from atomic context
  1355. */
  1356. int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  1357. struct list_head *head, void *data)
  1358. {
  1359. int err = 0;
  1360. efivar_entry_iter_begin();
  1361. err = __efivar_entry_iter(func, head, data, NULL);
  1362. efivar_entry_iter_end();
  1363. return err;
  1364. }
  1365. EXPORT_SYMBOL_GPL(efivar_entry_iter);
  1366. /**
  1367. * efivars_kobject - get the kobject for the registered efivars
  1368. *
  1369. * If efivars_register() has not been called we return NULL,
  1370. * otherwise return the kobject used at registration time.
  1371. */
  1372. struct kobject *efivars_kobject(void)
  1373. {
  1374. if (!__efivars)
  1375. return NULL;
  1376. return __efivars->kobject;
  1377. }
  1378. EXPORT_SYMBOL_GPL(efivars_kobject);
  1379. /**
  1380. * efivar_run_worker - schedule the efivar worker thread
  1381. */
  1382. void efivar_run_worker(void)
  1383. {
  1384. if (efivar_wq_enabled)
  1385. schedule_work(&efivar_work);
  1386. }
  1387. EXPORT_SYMBOL_GPL(efivar_run_worker);
  1388. /**
  1389. * efivars_register - register an efivars
  1390. * @efivars: efivars to register
  1391. * @ops: efivars operations
  1392. * @kobject: @efivars-specific kobject
  1393. *
  1394. * Only a single efivars can be registered at any time.
  1395. */
  1396. int efivars_register(struct efivars *efivars,
  1397. const struct efivar_operations *ops,
  1398. struct kobject *kobject)
  1399. {
  1400. spin_lock_init(&efivars->lock);
  1401. efivars->ops = ops;
  1402. efivars->kobject = kobject;
  1403. __efivars = efivars;
  1404. return 0;
  1405. }
  1406. EXPORT_SYMBOL_GPL(efivars_register);
  1407. /**
  1408. * efivars_unregister - unregister an efivars
  1409. * @efivars: efivars to unregister
  1410. *
  1411. * The caller must have already removed every entry from the list,
  1412. * failure to do so is an error.
  1413. */
  1414. int efivars_unregister(struct efivars *efivars)
  1415. {
  1416. int rv;
  1417. if (!__efivars) {
  1418. printk(KERN_ERR "efivars not registered\n");
  1419. rv = -EINVAL;
  1420. goto out;
  1421. }
  1422. if (__efivars != efivars) {
  1423. rv = -EINVAL;
  1424. goto out;
  1425. }
  1426. __efivars = NULL;
  1427. rv = 0;
  1428. out:
  1429. return rv;
  1430. }
  1431. EXPORT_SYMBOL_GPL(efivars_unregister);
  1432. static struct efivars generic_efivars;
  1433. static struct efivar_operations generic_ops;
  1434. static int generic_ops_register(void)
  1435. {
  1436. int error;
  1437. generic_ops.get_variable = efi.get_variable;
  1438. generic_ops.set_variable = efi.set_variable;
  1439. generic_ops.get_next_variable = efi.get_next_variable;
  1440. generic_ops.query_variable_info = efi.query_variable_info;
  1441. error = efivars_register(&generic_efivars, &generic_ops, efi_kobj);
  1442. if (error)
  1443. return error;
  1444. error = efivars_sysfs_init();
  1445. if (error)
  1446. efivars_unregister(&generic_efivars);
  1447. return error;
  1448. }
  1449. static void generic_ops_unregister(void)
  1450. {
  1451. efivars_sysfs_exit();
  1452. efivars_unregister(&generic_efivars);
  1453. }
  1454. /*
  1455. * For now we register the efi subsystem with the firmware subsystem
  1456. * and the vars subsystem with the efi subsystem. In the future, it
  1457. * might make sense to split off the efi subsystem into its own
  1458. * driver, but for now only efivars will register with it, so just
  1459. * include it here.
  1460. */
  1461. static int __init
  1462. efivars_init(void)
  1463. {
  1464. int error;
  1465. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  1466. return 0;
  1467. /* Register the efi directory at /sys/firmware/efi */
  1468. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1469. if (!efi_kobj) {
  1470. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1471. return -ENOMEM;
  1472. }
  1473. error = generic_ops_register();
  1474. if (error)
  1475. goto err_put;
  1476. /* Don't forget the systab entry */
  1477. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1478. if (error) {
  1479. printk(KERN_ERR
  1480. "efivars: Sysfs attribute export failed with error %d.\n",
  1481. error);
  1482. goto err_unregister;
  1483. }
  1484. return 0;
  1485. err_unregister:
  1486. generic_ops_unregister();
  1487. err_put:
  1488. kobject_put(efi_kobj);
  1489. return error;
  1490. }
  1491. static void __exit
  1492. efivars_exit(void)
  1493. {
  1494. cancel_work_sync(&efivar_work);
  1495. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  1496. generic_ops_unregister();
  1497. kobject_put(efi_kobj);
  1498. }
  1499. }
  1500. module_init(efivars_init);
  1501. module_exit(efivars_exit);