efivars.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  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/ctype.h>
  82. #include <linux/fs.h>
  83. #include <linux/ramfs.h>
  84. #include <linux/pagemap.h>
  85. #include <asm/uaccess.h>
  86. #define EFIVARS_VERSION "0.08"
  87. #define EFIVARS_DATE "2004-May-17"
  88. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  89. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  90. MODULE_LICENSE("GPL");
  91. MODULE_VERSION(EFIVARS_VERSION);
  92. #define DUMP_NAME_LEN 52
  93. /*
  94. * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
  95. * not including trailing NUL
  96. */
  97. #define GUID_LEN 36
  98. static bool efivars_pstore_disable =
  99. IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
  100. module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
  101. /*
  102. * The maximum size of VariableName + Data = 1024
  103. * Therefore, it's reasonable to save that much
  104. * space in each part of the structure,
  105. * and we use a page for reading/writing.
  106. */
  107. struct efi_variable {
  108. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  109. efi_guid_t VendorGuid;
  110. unsigned long DataSize;
  111. __u8 Data[1024];
  112. efi_status_t Status;
  113. __u32 Attributes;
  114. } __attribute__((packed));
  115. struct efivar_entry {
  116. struct efivars *efivars;
  117. struct efi_variable var;
  118. struct list_head list;
  119. struct kobject kobj;
  120. };
  121. struct efivar_attribute {
  122. struct attribute attr;
  123. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  124. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  125. };
  126. static struct efivars __efivars;
  127. static struct efivar_operations ops;
  128. #define PSTORE_EFI_ATTRIBUTES \
  129. (EFI_VARIABLE_NON_VOLATILE | \
  130. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  131. EFI_VARIABLE_RUNTIME_ACCESS)
  132. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  133. struct efivar_attribute efivar_attr_##_name = { \
  134. .attr = {.name = __stringify(_name), .mode = _mode}, \
  135. .show = _show, \
  136. .store = _store, \
  137. };
  138. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  139. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  140. /*
  141. * Prototype for sysfs creation function
  142. */
  143. static int
  144. efivar_create_sysfs_entry(struct efivars *efivars,
  145. unsigned long variable_name_size,
  146. efi_char16_t *variable_name,
  147. efi_guid_t *vendor_guid);
  148. /*
  149. * Prototype for workqueue functions updating sysfs entry
  150. */
  151. static void efivar_update_sysfs_entries(struct work_struct *);
  152. static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
  153. static bool efivar_wq_enabled = true;
  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. unsigned long flags;
  346. spin_lock_irqsave(&efivars->lock, flags);
  347. status = get_var_data_locked(efivars, var);
  348. spin_unlock_irqrestore(&efivars->lock, flags);
  349. if (status != EFI_SUCCESS) {
  350. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  351. status);
  352. }
  353. return status;
  354. }
  355. static efi_status_t
  356. check_var_size_locked(struct efivars *efivars, u32 attributes,
  357. unsigned long size)
  358. {
  359. u64 storage_size, remaining_size, max_size;
  360. efi_status_t status;
  361. const struct efivar_operations *fops = efivars->ops;
  362. if (!efivars->ops->query_variable_info)
  363. return EFI_UNSUPPORTED;
  364. status = fops->query_variable_info(attributes, &storage_size,
  365. &remaining_size, &max_size);
  366. if (status != EFI_SUCCESS)
  367. return status;
  368. if (!storage_size || size > remaining_size || size > max_size ||
  369. (remaining_size - size) < (storage_size / 2))
  370. return EFI_OUT_OF_RESOURCES;
  371. return status;
  372. }
  373. static efi_status_t
  374. check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
  375. {
  376. efi_status_t status;
  377. unsigned long flags;
  378. spin_lock_irqsave(&efivars->lock, flags);
  379. status = check_var_size_locked(efivars, attributes, size);
  380. spin_unlock_irqrestore(&efivars->lock, flags);
  381. return status;
  382. }
  383. static ssize_t
  384. efivar_guid_read(struct efivar_entry *entry, char *buf)
  385. {
  386. struct efi_variable *var = &entry->var;
  387. char *str = buf;
  388. if (!entry || !buf)
  389. return 0;
  390. efi_guid_unparse(&var->VendorGuid, str);
  391. str += strlen(str);
  392. str += sprintf(str, "\n");
  393. return str - buf;
  394. }
  395. static ssize_t
  396. efivar_attr_read(struct efivar_entry *entry, char *buf)
  397. {
  398. struct efi_variable *var = &entry->var;
  399. char *str = buf;
  400. efi_status_t status;
  401. if (!entry || !buf)
  402. return -EINVAL;
  403. status = get_var_data(entry->efivars, var);
  404. if (status != EFI_SUCCESS)
  405. return -EIO;
  406. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  407. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  408. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  409. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  410. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  411. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  412. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  413. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  414. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  415. str += sprintf(str,
  416. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  417. if (var->Attributes &
  418. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  419. str += sprintf(str,
  420. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  421. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  422. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  423. return str - buf;
  424. }
  425. static ssize_t
  426. efivar_size_read(struct efivar_entry *entry, char *buf)
  427. {
  428. struct efi_variable *var = &entry->var;
  429. char *str = buf;
  430. efi_status_t status;
  431. if (!entry || !buf)
  432. return -EINVAL;
  433. status = get_var_data(entry->efivars, var);
  434. if (status != EFI_SUCCESS)
  435. return -EIO;
  436. str += sprintf(str, "0x%lx\n", var->DataSize);
  437. return str - buf;
  438. }
  439. static ssize_t
  440. efivar_data_read(struct efivar_entry *entry, char *buf)
  441. {
  442. struct efi_variable *var = &entry->var;
  443. efi_status_t status;
  444. if (!entry || !buf)
  445. return -EINVAL;
  446. status = get_var_data(entry->efivars, var);
  447. if (status != EFI_SUCCESS)
  448. return -EIO;
  449. memcpy(buf, var->Data, var->DataSize);
  450. return var->DataSize;
  451. }
  452. /*
  453. * We allow each variable to be edited via rewriting the
  454. * entire efi variable structure.
  455. */
  456. static ssize_t
  457. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  458. {
  459. struct efi_variable *new_var, *var = &entry->var;
  460. struct efivars *efivars = entry->efivars;
  461. efi_status_t status = EFI_NOT_FOUND;
  462. if (count != sizeof(struct efi_variable))
  463. return -EINVAL;
  464. new_var = (struct efi_variable *)buf;
  465. /*
  466. * If only updating the variable data, then the name
  467. * and guid should remain the same
  468. */
  469. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  470. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  471. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  472. return -EINVAL;
  473. }
  474. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  475. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  476. return -EINVAL;
  477. }
  478. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  479. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  480. printk(KERN_ERR "efivars: Malformed variable content\n");
  481. return -EINVAL;
  482. }
  483. spin_lock_irq(&efivars->lock);
  484. status = check_var_size_locked(efivars, new_var->Attributes,
  485. new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
  486. if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
  487. status = efivars->ops->set_variable(new_var->VariableName,
  488. &new_var->VendorGuid,
  489. new_var->Attributes,
  490. new_var->DataSize,
  491. new_var->Data);
  492. spin_unlock_irq(&efivars->lock);
  493. if (status != EFI_SUCCESS) {
  494. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  495. status);
  496. return -EIO;
  497. }
  498. memcpy(&entry->var, new_var, count);
  499. return count;
  500. }
  501. static ssize_t
  502. efivar_show_raw(struct efivar_entry *entry, char *buf)
  503. {
  504. struct efi_variable *var = &entry->var;
  505. efi_status_t status;
  506. if (!entry || !buf)
  507. return 0;
  508. status = get_var_data(entry->efivars, var);
  509. if (status != EFI_SUCCESS)
  510. return -EIO;
  511. memcpy(buf, var, sizeof(*var));
  512. return sizeof(*var);
  513. }
  514. /*
  515. * Generic read/write functions that call the specific functions of
  516. * the attributes...
  517. */
  518. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  519. char *buf)
  520. {
  521. struct efivar_entry *var = to_efivar_entry(kobj);
  522. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  523. ssize_t ret = -EIO;
  524. if (!capable(CAP_SYS_ADMIN))
  525. return -EACCES;
  526. if (efivar_attr->show) {
  527. ret = efivar_attr->show(var, buf);
  528. }
  529. return ret;
  530. }
  531. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  532. const char *buf, size_t count)
  533. {
  534. struct efivar_entry *var = to_efivar_entry(kobj);
  535. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  536. ssize_t ret = -EIO;
  537. if (!capable(CAP_SYS_ADMIN))
  538. return -EACCES;
  539. if (efivar_attr->store)
  540. ret = efivar_attr->store(var, buf, count);
  541. return ret;
  542. }
  543. static const struct sysfs_ops efivar_attr_ops = {
  544. .show = efivar_attr_show,
  545. .store = efivar_attr_store,
  546. };
  547. static void efivar_release(struct kobject *kobj)
  548. {
  549. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  550. kfree(var);
  551. }
  552. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  553. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  554. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  555. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  556. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  557. static struct attribute *def_attrs[] = {
  558. &efivar_attr_guid.attr,
  559. &efivar_attr_size.attr,
  560. &efivar_attr_attributes.attr,
  561. &efivar_attr_data.attr,
  562. &efivar_attr_raw_var.attr,
  563. NULL,
  564. };
  565. static struct kobj_type efivar_ktype = {
  566. .release = efivar_release,
  567. .sysfs_ops = &efivar_attr_ops,
  568. .default_attrs = def_attrs,
  569. };
  570. static inline void
  571. efivar_unregister(struct efivar_entry *var)
  572. {
  573. kobject_put(&var->kobj);
  574. }
  575. static int efivarfs_file_open(struct inode *inode, struct file *file)
  576. {
  577. file->private_data = inode->i_private;
  578. return 0;
  579. }
  580. static int efi_status_to_err(efi_status_t status)
  581. {
  582. int err;
  583. switch (status) {
  584. case EFI_INVALID_PARAMETER:
  585. err = -EINVAL;
  586. break;
  587. case EFI_OUT_OF_RESOURCES:
  588. err = -ENOSPC;
  589. break;
  590. case EFI_DEVICE_ERROR:
  591. err = -EIO;
  592. break;
  593. case EFI_WRITE_PROTECTED:
  594. err = -EROFS;
  595. break;
  596. case EFI_SECURITY_VIOLATION:
  597. err = -EACCES;
  598. break;
  599. case EFI_NOT_FOUND:
  600. err = -EIO;
  601. break;
  602. default:
  603. err = -EINVAL;
  604. }
  605. return err;
  606. }
  607. static ssize_t efivarfs_file_write(struct file *file,
  608. const char __user *userbuf, size_t count, loff_t *ppos)
  609. {
  610. struct efivar_entry *var = file->private_data;
  611. struct efivars *efivars;
  612. efi_status_t status;
  613. void *data;
  614. u32 attributes;
  615. struct inode *inode = file->f_mapping->host;
  616. unsigned long datasize = count - sizeof(attributes);
  617. unsigned long newdatasize, varsize;
  618. ssize_t bytes = 0;
  619. if (count < sizeof(attributes))
  620. return -EINVAL;
  621. if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
  622. return -EFAULT;
  623. if (attributes & ~(EFI_VARIABLE_MASK))
  624. return -EINVAL;
  625. efivars = var->efivars;
  626. /*
  627. * Ensure that the user can't allocate arbitrarily large
  628. * amounts of memory. Pick a default size of 64K if
  629. * QueryVariableInfo() isn't supported by the firmware.
  630. */
  631. varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
  632. status = check_var_size(efivars, attributes, varsize);
  633. if (status != EFI_SUCCESS) {
  634. if (status != EFI_UNSUPPORTED)
  635. return efi_status_to_err(status);
  636. if (datasize > 65536)
  637. return -ENOSPC;
  638. }
  639. data = kmalloc(datasize, GFP_KERNEL);
  640. if (!data)
  641. return -ENOMEM;
  642. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  643. bytes = -EFAULT;
  644. goto out;
  645. }
  646. if (validate_var(&var->var, data, datasize) == false) {
  647. bytes = -EINVAL;
  648. goto out;
  649. }
  650. /*
  651. * The lock here protects the get_variable call, the conditional
  652. * set_variable call, and removal of the variable from the efivars
  653. * list (in the case of an authenticated delete).
  654. */
  655. spin_lock_irq(&efivars->lock);
  656. /*
  657. * Ensure that the available space hasn't shrunk below the safe level
  658. */
  659. status = check_var_size_locked(efivars, attributes, varsize);
  660. if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
  661. spin_unlock_irq(&efivars->lock);
  662. kfree(data);
  663. return efi_status_to_err(status);
  664. }
  665. status = efivars->ops->set_variable(var->var.VariableName,
  666. &var->var.VendorGuid,
  667. attributes, datasize,
  668. data);
  669. if (status != EFI_SUCCESS) {
  670. spin_unlock_irq(&efivars->lock);
  671. kfree(data);
  672. return efi_status_to_err(status);
  673. }
  674. bytes = count;
  675. /*
  676. * Writing to the variable may have caused a change in size (which
  677. * could either be an append or an overwrite), or the variable to be
  678. * deleted. Perform a GetVariable() so we can tell what actually
  679. * happened.
  680. */
  681. newdatasize = 0;
  682. status = efivars->ops->get_variable(var->var.VariableName,
  683. &var->var.VendorGuid,
  684. NULL, &newdatasize,
  685. NULL);
  686. if (status == EFI_BUFFER_TOO_SMALL) {
  687. spin_unlock_irq(&efivars->lock);
  688. mutex_lock(&inode->i_mutex);
  689. i_size_write(inode, newdatasize + sizeof(attributes));
  690. mutex_unlock(&inode->i_mutex);
  691. } else if (status == EFI_NOT_FOUND) {
  692. list_del(&var->list);
  693. spin_unlock_irq(&efivars->lock);
  694. efivar_unregister(var);
  695. drop_nlink(inode);
  696. d_delete(file->f_dentry);
  697. dput(file->f_dentry);
  698. } else {
  699. spin_unlock_irq(&efivars->lock);
  700. pr_warn("efivarfs: inconsistent EFI variable implementation? "
  701. "status = %lx\n", status);
  702. }
  703. out:
  704. kfree(data);
  705. return bytes;
  706. }
  707. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  708. size_t count, loff_t *ppos)
  709. {
  710. struct efivar_entry *var = file->private_data;
  711. struct efivars *efivars = var->efivars;
  712. efi_status_t status;
  713. unsigned long datasize = 0;
  714. u32 attributes;
  715. void *data;
  716. ssize_t size = 0;
  717. spin_lock_irq(&efivars->lock);
  718. status = efivars->ops->get_variable(var->var.VariableName,
  719. &var->var.VendorGuid,
  720. &attributes, &datasize, NULL);
  721. spin_unlock_irq(&efivars->lock);
  722. if (status != EFI_BUFFER_TOO_SMALL)
  723. return efi_status_to_err(status);
  724. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  725. if (!data)
  726. return -ENOMEM;
  727. spin_lock_irq(&efivars->lock);
  728. status = efivars->ops->get_variable(var->var.VariableName,
  729. &var->var.VendorGuid,
  730. &attributes, &datasize,
  731. (data + sizeof(attributes)));
  732. spin_unlock_irq(&efivars->lock);
  733. if (status != EFI_SUCCESS) {
  734. size = efi_status_to_err(status);
  735. goto out_free;
  736. }
  737. memcpy(data, &attributes, sizeof(attributes));
  738. size = simple_read_from_buffer(userbuf, count, ppos,
  739. data, datasize + sizeof(attributes));
  740. out_free:
  741. kfree(data);
  742. return size;
  743. }
  744. static void efivarfs_evict_inode(struct inode *inode)
  745. {
  746. clear_inode(inode);
  747. }
  748. static const struct super_operations efivarfs_ops = {
  749. .statfs = simple_statfs,
  750. .drop_inode = generic_delete_inode,
  751. .evict_inode = efivarfs_evict_inode,
  752. .show_options = generic_show_options,
  753. };
  754. static struct super_block *efivarfs_sb;
  755. static const struct inode_operations efivarfs_dir_inode_operations;
  756. static const struct file_operations efivarfs_file_operations = {
  757. .open = efivarfs_file_open,
  758. .read = efivarfs_file_read,
  759. .write = efivarfs_file_write,
  760. .llseek = no_llseek,
  761. };
  762. static struct inode *efivarfs_get_inode(struct super_block *sb,
  763. const struct inode *dir, int mode, dev_t dev)
  764. {
  765. struct inode *inode = new_inode(sb);
  766. if (inode) {
  767. inode->i_ino = get_next_ino();
  768. inode->i_mode = mode;
  769. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  770. switch (mode & S_IFMT) {
  771. case S_IFREG:
  772. inode->i_fop = &efivarfs_file_operations;
  773. break;
  774. case S_IFDIR:
  775. inode->i_op = &efivarfs_dir_inode_operations;
  776. inode->i_fop = &simple_dir_operations;
  777. inc_nlink(inode);
  778. break;
  779. }
  780. }
  781. return inode;
  782. }
  783. /*
  784. * Return true if 'str' is a valid efivarfs filename of the form,
  785. *
  786. * VariableName-12345678-1234-1234-1234-1234567891bc
  787. */
  788. static bool efivarfs_valid_name(const char *str, int len)
  789. {
  790. static const char dashes[GUID_LEN] = {
  791. [8] = 1, [13] = 1, [18] = 1, [23] = 1
  792. };
  793. const char *s = str + len - GUID_LEN;
  794. int i;
  795. /*
  796. * We need a GUID, plus at least one letter for the variable name,
  797. * plus the '-' separator
  798. */
  799. if (len < GUID_LEN + 2)
  800. return false;
  801. /* GUID must be preceded by a '-' */
  802. if (*(s - 1) != '-')
  803. return false;
  804. /*
  805. * Validate that 's' is of the correct format, e.g.
  806. *
  807. * 12345678-1234-1234-1234-123456789abc
  808. */
  809. for (i = 0; i < GUID_LEN; i++) {
  810. if (dashes[i]) {
  811. if (*s++ != '-')
  812. return false;
  813. } else {
  814. if (!isxdigit(*s++))
  815. return false;
  816. }
  817. }
  818. return true;
  819. }
  820. static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
  821. {
  822. guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
  823. guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
  824. guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
  825. guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
  826. guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
  827. guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
  828. guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
  829. guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
  830. guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
  831. guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
  832. guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
  833. guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
  834. guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
  835. guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
  836. guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
  837. guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
  838. }
  839. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  840. umode_t mode, bool excl)
  841. {
  842. struct inode *inode;
  843. struct efivars *efivars = &__efivars;
  844. struct efivar_entry *var;
  845. int namelen, i = 0, err = 0;
  846. if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
  847. return -EINVAL;
  848. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
  849. if (!inode)
  850. return -ENOMEM;
  851. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  852. if (!var) {
  853. err = -ENOMEM;
  854. goto out;
  855. }
  856. /* length of the variable name itself: remove GUID and separator */
  857. namelen = dentry->d_name.len - GUID_LEN - 1;
  858. efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
  859. &var->var.VendorGuid);
  860. for (i = 0; i < namelen; i++)
  861. var->var.VariableName[i] = dentry->d_name.name[i];
  862. var->var.VariableName[i] = '\0';
  863. inode->i_private = var;
  864. var->efivars = efivars;
  865. var->kobj.kset = efivars->kset;
  866. err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
  867. dentry->d_name.name);
  868. if (err)
  869. goto out;
  870. kobject_uevent(&var->kobj, KOBJ_ADD);
  871. spin_lock_irq(&efivars->lock);
  872. list_add(&var->list, &efivars->list);
  873. spin_unlock_irq(&efivars->lock);
  874. d_instantiate(dentry, inode);
  875. dget(dentry);
  876. out:
  877. if (err) {
  878. kfree(var);
  879. iput(inode);
  880. }
  881. return err;
  882. }
  883. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  884. {
  885. struct efivar_entry *var = dentry->d_inode->i_private;
  886. struct efivars *efivars = var->efivars;
  887. efi_status_t status;
  888. spin_lock_irq(&efivars->lock);
  889. status = efivars->ops->set_variable(var->var.VariableName,
  890. &var->var.VendorGuid,
  891. 0, 0, NULL);
  892. if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
  893. list_del(&var->list);
  894. spin_unlock_irq(&efivars->lock);
  895. efivar_unregister(var);
  896. drop_nlink(dentry->d_inode);
  897. dput(dentry);
  898. return 0;
  899. }
  900. spin_unlock_irq(&efivars->lock);
  901. return -EINVAL;
  902. };
  903. /*
  904. * Compare two efivarfs file names.
  905. *
  906. * An efivarfs filename is composed of two parts,
  907. *
  908. * 1. A case-sensitive variable name
  909. * 2. A case-insensitive GUID
  910. *
  911. * So we need to perform a case-sensitive match on part 1 and a
  912. * case-insensitive match on part 2.
  913. */
  914. static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
  915. const struct dentry *dentry, const struct inode *inode,
  916. unsigned int len, const char *str,
  917. const struct qstr *name)
  918. {
  919. int guid = len - GUID_LEN;
  920. if (name->len != len)
  921. return 1;
  922. /* Case-sensitive compare for the variable name */
  923. if (memcmp(str, name->name, guid))
  924. return 1;
  925. /* Case-insensitive compare for the GUID */
  926. return strncasecmp(name->name + guid, str + guid, GUID_LEN);
  927. }
  928. static int efivarfs_d_hash(const struct dentry *dentry,
  929. const struct inode *inode, struct qstr *qstr)
  930. {
  931. unsigned long hash = init_name_hash();
  932. const unsigned char *s = qstr->name;
  933. unsigned int len = qstr->len;
  934. if (!efivarfs_valid_name(s, len))
  935. return -EINVAL;
  936. while (len-- > GUID_LEN)
  937. hash = partial_name_hash(*s++, hash);
  938. /* GUID is case-insensitive. */
  939. while (len--)
  940. hash = partial_name_hash(tolower(*s++), hash);
  941. qstr->hash = end_name_hash(hash);
  942. return 0;
  943. }
  944. /*
  945. * Retaining negative dentries for an in-memory filesystem just wastes
  946. * memory and lookup time: arrange for them to be deleted immediately.
  947. */
  948. static int efivarfs_delete_dentry(const struct dentry *dentry)
  949. {
  950. return 1;
  951. }
  952. static struct dentry_operations efivarfs_d_ops = {
  953. .d_compare = efivarfs_d_compare,
  954. .d_hash = efivarfs_d_hash,
  955. .d_delete = efivarfs_delete_dentry,
  956. };
  957. static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
  958. {
  959. struct dentry *d;
  960. struct qstr q;
  961. int err;
  962. q.name = name;
  963. q.len = strlen(name);
  964. err = efivarfs_d_hash(NULL, NULL, &q);
  965. if (err)
  966. return ERR_PTR(err);
  967. d = d_alloc(parent, &q);
  968. if (d)
  969. return d;
  970. return ERR_PTR(-ENOMEM);
  971. }
  972. static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  973. {
  974. struct inode *inode = NULL;
  975. struct dentry *root;
  976. struct efivar_entry *entry, *n;
  977. struct efivars *efivars = &__efivars;
  978. char *name;
  979. int err = -ENOMEM;
  980. efivarfs_sb = sb;
  981. sb->s_maxbytes = MAX_LFS_FILESIZE;
  982. sb->s_blocksize = PAGE_CACHE_SIZE;
  983. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  984. sb->s_magic = EFIVARFS_MAGIC;
  985. sb->s_op = &efivarfs_ops;
  986. sb->s_d_op = &efivarfs_d_ops;
  987. sb->s_time_gran = 1;
  988. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  989. if (!inode)
  990. return -ENOMEM;
  991. inode->i_op = &efivarfs_dir_inode_operations;
  992. root = d_make_root(inode);
  993. sb->s_root = root;
  994. if (!root)
  995. return -ENOMEM;
  996. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  997. struct dentry *dentry, *root = efivarfs_sb->s_root;
  998. unsigned long size = 0;
  999. int len, i;
  1000. inode = NULL;
  1001. len = utf16_strlen(entry->var.VariableName);
  1002. /* name, plus '-', plus GUID, plus NUL*/
  1003. name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
  1004. if (!name)
  1005. goto fail;
  1006. for (i = 0; i < len; i++)
  1007. name[i] = entry->var.VariableName[i] & 0xFF;
  1008. name[len] = '-';
  1009. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  1010. name[len+GUID_LEN+1] = '\0';
  1011. inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
  1012. S_IFREG | 0644, 0);
  1013. if (!inode)
  1014. goto fail_name;
  1015. dentry = efivarfs_alloc_dentry(root, name);
  1016. if (IS_ERR(dentry)) {
  1017. err = PTR_ERR(dentry);
  1018. goto fail_inode;
  1019. }
  1020. /* copied by the above to local storage in the dentry. */
  1021. kfree(name);
  1022. spin_lock_irq(&efivars->lock);
  1023. efivars->ops->get_variable(entry->var.VariableName,
  1024. &entry->var.VendorGuid,
  1025. &entry->var.Attributes,
  1026. &size,
  1027. NULL);
  1028. spin_unlock_irq(&efivars->lock);
  1029. mutex_lock(&inode->i_mutex);
  1030. inode->i_private = entry;
  1031. i_size_write(inode, size + sizeof(entry->var.Attributes));
  1032. mutex_unlock(&inode->i_mutex);
  1033. d_add(dentry, inode);
  1034. }
  1035. return 0;
  1036. fail_inode:
  1037. iput(inode);
  1038. fail_name:
  1039. kfree(name);
  1040. fail:
  1041. return err;
  1042. }
  1043. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  1044. int flags, const char *dev_name, void *data)
  1045. {
  1046. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  1047. }
  1048. static void efivarfs_kill_sb(struct super_block *sb)
  1049. {
  1050. kill_litter_super(sb);
  1051. efivarfs_sb = NULL;
  1052. }
  1053. static struct file_system_type efivarfs_type = {
  1054. .name = "efivarfs",
  1055. .mount = efivarfs_mount,
  1056. .kill_sb = efivarfs_kill_sb,
  1057. };
  1058. MODULE_ALIAS_FS("efivarfs");
  1059. /*
  1060. * Handle negative dentry.
  1061. */
  1062. static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
  1063. unsigned int flags)
  1064. {
  1065. if (dentry->d_name.len > NAME_MAX)
  1066. return ERR_PTR(-ENAMETOOLONG);
  1067. d_add(dentry, NULL);
  1068. return NULL;
  1069. }
  1070. static const struct inode_operations efivarfs_dir_inode_operations = {
  1071. .lookup = efivarfs_lookup,
  1072. .unlink = efivarfs_unlink,
  1073. .create = efivarfs_create,
  1074. };
  1075. #ifdef CONFIG_EFI_VARS_PSTORE
  1076. static int efi_pstore_open(struct pstore_info *psi)
  1077. {
  1078. struct efivars *efivars = psi->data;
  1079. spin_lock_irq(&efivars->lock);
  1080. efivars->walk_entry = list_first_entry(&efivars->list,
  1081. struct efivar_entry, list);
  1082. return 0;
  1083. }
  1084. static int efi_pstore_close(struct pstore_info *psi)
  1085. {
  1086. struct efivars *efivars = psi->data;
  1087. spin_unlock_irq(&efivars->lock);
  1088. return 0;
  1089. }
  1090. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  1091. int *count, struct timespec *timespec,
  1092. char **buf, struct pstore_info *psi)
  1093. {
  1094. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1095. struct efivars *efivars = psi->data;
  1096. char name[DUMP_NAME_LEN];
  1097. int i;
  1098. int cnt;
  1099. unsigned int part, size;
  1100. unsigned long time;
  1101. while (&efivars->walk_entry->list != &efivars->list) {
  1102. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  1103. vendor)) {
  1104. for (i = 0; i < DUMP_NAME_LEN; i++) {
  1105. name[i] = efivars->walk_entry->var.VariableName[i];
  1106. }
  1107. if (sscanf(name, "dump-type%u-%u-%d-%lu",
  1108. type, &part, &cnt, &time) == 4) {
  1109. *id = part;
  1110. *count = cnt;
  1111. timespec->tv_sec = time;
  1112. timespec->tv_nsec = 0;
  1113. } else if (sscanf(name, "dump-type%u-%u-%lu",
  1114. type, &part, &time) == 3) {
  1115. /*
  1116. * Check if an old format,
  1117. * which doesn't support holding
  1118. * multiple logs, remains.
  1119. */
  1120. *id = part;
  1121. *count = 0;
  1122. timespec->tv_sec = time;
  1123. timespec->tv_nsec = 0;
  1124. } else {
  1125. efivars->walk_entry = list_entry(
  1126. efivars->walk_entry->list.next,
  1127. struct efivar_entry, list);
  1128. continue;
  1129. }
  1130. get_var_data_locked(efivars, &efivars->walk_entry->var);
  1131. size = efivars->walk_entry->var.DataSize;
  1132. *buf = kmalloc(size, GFP_KERNEL);
  1133. if (*buf == NULL)
  1134. return -ENOMEM;
  1135. memcpy(*buf, efivars->walk_entry->var.Data,
  1136. size);
  1137. efivars->walk_entry = list_entry(
  1138. efivars->walk_entry->list.next,
  1139. struct efivar_entry, list);
  1140. return size;
  1141. }
  1142. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  1143. struct efivar_entry, list);
  1144. }
  1145. return 0;
  1146. }
  1147. static int efi_pstore_write(enum pstore_type_id type,
  1148. enum kmsg_dump_reason reason, u64 *id,
  1149. unsigned int part, int count, size_t size,
  1150. struct pstore_info *psi)
  1151. {
  1152. char name[DUMP_NAME_LEN];
  1153. efi_char16_t efi_name[DUMP_NAME_LEN];
  1154. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1155. struct efivars *efivars = psi->data;
  1156. int i, ret = 0;
  1157. efi_status_t status = EFI_NOT_FOUND;
  1158. unsigned long flags;
  1159. if (pstore_cannot_block_path(reason)) {
  1160. /*
  1161. * If the lock is taken by another cpu in non-blocking path,
  1162. * this driver returns without entering firmware to avoid
  1163. * hanging up.
  1164. */
  1165. if (!spin_trylock_irqsave(&efivars->lock, flags))
  1166. return -EBUSY;
  1167. } else
  1168. spin_lock_irqsave(&efivars->lock, flags);
  1169. /*
  1170. * Check if there is a space enough to log.
  1171. * size: a size of logging data
  1172. * DUMP_NAME_LEN * 2: a maximum size of variable name
  1173. */
  1174. status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
  1175. size + DUMP_NAME_LEN * 2);
  1176. if (status) {
  1177. spin_unlock_irqrestore(&efivars->lock, flags);
  1178. *id = part;
  1179. return -ENOSPC;
  1180. }
  1181. sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
  1182. get_seconds());
  1183. for (i = 0; i < DUMP_NAME_LEN; i++)
  1184. efi_name[i] = name[i];
  1185. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  1186. size, psi->buf);
  1187. spin_unlock_irqrestore(&efivars->lock, flags);
  1188. if (reason == KMSG_DUMP_OOPS && efivar_wq_enabled)
  1189. schedule_work(&efivar_work);
  1190. *id = part;
  1191. return ret;
  1192. };
  1193. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  1194. struct timespec time, struct pstore_info *psi)
  1195. {
  1196. char name[DUMP_NAME_LEN];
  1197. efi_char16_t efi_name[DUMP_NAME_LEN];
  1198. char name_old[DUMP_NAME_LEN];
  1199. efi_char16_t efi_name_old[DUMP_NAME_LEN];
  1200. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  1201. struct efivars *efivars = psi->data;
  1202. struct efivar_entry *entry, *found = NULL;
  1203. int i;
  1204. sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
  1205. time.tv_sec);
  1206. spin_lock_irq(&efivars->lock);
  1207. for (i = 0; i < DUMP_NAME_LEN; i++)
  1208. efi_name[i] = name[i];
  1209. /*
  1210. * Clean up an entry with the same name
  1211. */
  1212. list_for_each_entry(entry, &efivars->list, list) {
  1213. get_var_data_locked(efivars, &entry->var);
  1214. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  1215. continue;
  1216. if (utf16_strncmp(entry->var.VariableName, efi_name,
  1217. utf16_strlen(efi_name))) {
  1218. /*
  1219. * Check if an old format,
  1220. * which doesn't support holding
  1221. * multiple logs, remains.
  1222. */
  1223. sprintf(name_old, "dump-type%u-%u-%lu", type,
  1224. (unsigned int)id, time.tv_sec);
  1225. for (i = 0; i < DUMP_NAME_LEN; i++)
  1226. efi_name_old[i] = name_old[i];
  1227. if (utf16_strncmp(entry->var.VariableName, efi_name_old,
  1228. utf16_strlen(efi_name_old)))
  1229. continue;
  1230. }
  1231. /* found */
  1232. found = entry;
  1233. efivars->ops->set_variable(entry->var.VariableName,
  1234. &entry->var.VendorGuid,
  1235. PSTORE_EFI_ATTRIBUTES,
  1236. 0, NULL);
  1237. break;
  1238. }
  1239. if (found)
  1240. list_del(&found->list);
  1241. spin_unlock_irq(&efivars->lock);
  1242. if (found)
  1243. efivar_unregister(found);
  1244. return 0;
  1245. }
  1246. static struct pstore_info efi_pstore_info = {
  1247. .owner = THIS_MODULE,
  1248. .name = "efi",
  1249. .open = efi_pstore_open,
  1250. .close = efi_pstore_close,
  1251. .read = efi_pstore_read,
  1252. .write = efi_pstore_write,
  1253. .erase = efi_pstore_erase,
  1254. };
  1255. static void efivar_pstore_register(struct efivars *efivars)
  1256. {
  1257. efivars->efi_pstore_info = efi_pstore_info;
  1258. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1259. if (efivars->efi_pstore_info.buf) {
  1260. efivars->efi_pstore_info.bufsize = 1024;
  1261. efivars->efi_pstore_info.data = efivars;
  1262. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1263. pstore_register(&efivars->efi_pstore_info);
  1264. }
  1265. }
  1266. #else
  1267. static void efivar_pstore_register(struct efivars *efivars)
  1268. {
  1269. return;
  1270. }
  1271. #endif
  1272. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  1273. struct bin_attribute *bin_attr,
  1274. char *buf, loff_t pos, size_t count)
  1275. {
  1276. struct efi_variable *new_var = (struct efi_variable *)buf;
  1277. struct efivars *efivars = bin_attr->private;
  1278. struct efivar_entry *search_efivar, *n;
  1279. unsigned long strsize1, strsize2;
  1280. efi_status_t status = EFI_NOT_FOUND;
  1281. int found = 0;
  1282. if (!capable(CAP_SYS_ADMIN))
  1283. return -EACCES;
  1284. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  1285. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  1286. printk(KERN_ERR "efivars: Malformed variable content\n");
  1287. return -EINVAL;
  1288. }
  1289. spin_lock_irq(&efivars->lock);
  1290. /*
  1291. * Does this variable already exist?
  1292. */
  1293. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1294. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1295. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  1296. if (strsize1 == strsize2 &&
  1297. !memcmp(&(search_efivar->var.VariableName),
  1298. new_var->VariableName, strsize1) &&
  1299. !efi_guidcmp(search_efivar->var.VendorGuid,
  1300. new_var->VendorGuid)) {
  1301. found = 1;
  1302. break;
  1303. }
  1304. }
  1305. if (found) {
  1306. spin_unlock_irq(&efivars->lock);
  1307. return -EINVAL;
  1308. }
  1309. status = check_var_size_locked(efivars, new_var->Attributes,
  1310. new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
  1311. if (status && status != EFI_UNSUPPORTED) {
  1312. spin_unlock_irq(&efivars->lock);
  1313. return efi_status_to_err(status);
  1314. }
  1315. /* now *really* create the variable via EFI */
  1316. status = efivars->ops->set_variable(new_var->VariableName,
  1317. &new_var->VendorGuid,
  1318. new_var->Attributes,
  1319. new_var->DataSize,
  1320. new_var->Data);
  1321. if (status != EFI_SUCCESS) {
  1322. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1323. status);
  1324. spin_unlock_irq(&efivars->lock);
  1325. return -EIO;
  1326. }
  1327. spin_unlock_irq(&efivars->lock);
  1328. /* Create the entry in sysfs. Locking is not required here */
  1329. status = efivar_create_sysfs_entry(efivars,
  1330. utf16_strsize(new_var->VariableName,
  1331. 1024),
  1332. new_var->VariableName,
  1333. &new_var->VendorGuid);
  1334. if (status) {
  1335. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  1336. }
  1337. return count;
  1338. }
  1339. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  1340. struct bin_attribute *bin_attr,
  1341. char *buf, loff_t pos, size_t count)
  1342. {
  1343. struct efi_variable *del_var = (struct efi_variable *)buf;
  1344. struct efivars *efivars = bin_attr->private;
  1345. struct efivar_entry *search_efivar, *n;
  1346. unsigned long strsize1, strsize2;
  1347. efi_status_t status = EFI_NOT_FOUND;
  1348. int found = 0;
  1349. if (!capable(CAP_SYS_ADMIN))
  1350. return -EACCES;
  1351. spin_lock_irq(&efivars->lock);
  1352. /*
  1353. * Does this variable already exist?
  1354. */
  1355. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  1356. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  1357. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  1358. if (strsize1 == strsize2 &&
  1359. !memcmp(&(search_efivar->var.VariableName),
  1360. del_var->VariableName, strsize1) &&
  1361. !efi_guidcmp(search_efivar->var.VendorGuid,
  1362. del_var->VendorGuid)) {
  1363. found = 1;
  1364. break;
  1365. }
  1366. }
  1367. if (!found) {
  1368. spin_unlock_irq(&efivars->lock);
  1369. return -EINVAL;
  1370. }
  1371. /* force the Attributes/DataSize to 0 to ensure deletion */
  1372. del_var->Attributes = 0;
  1373. del_var->DataSize = 0;
  1374. status = efivars->ops->set_variable(del_var->VariableName,
  1375. &del_var->VendorGuid,
  1376. del_var->Attributes,
  1377. del_var->DataSize,
  1378. del_var->Data);
  1379. if (status != EFI_SUCCESS) {
  1380. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  1381. status);
  1382. spin_unlock_irq(&efivars->lock);
  1383. return -EIO;
  1384. }
  1385. list_del(&search_efivar->list);
  1386. /* We need to release this lock before unregistering. */
  1387. spin_unlock_irq(&efivars->lock);
  1388. efivar_unregister(search_efivar);
  1389. /* It's dead Jim.... */
  1390. return count;
  1391. }
  1392. static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
  1393. {
  1394. struct efivar_entry *entry, *n;
  1395. struct efivars *efivars = &__efivars;
  1396. unsigned long strsize1, strsize2;
  1397. bool found = false;
  1398. strsize1 = utf16_strsize(variable_name, 1024);
  1399. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1400. strsize2 = utf16_strsize(entry->var.VariableName, 1024);
  1401. if (strsize1 == strsize2 &&
  1402. !memcmp(variable_name, &(entry->var.VariableName),
  1403. strsize2) &&
  1404. !efi_guidcmp(entry->var.VendorGuid,
  1405. *vendor)) {
  1406. found = true;
  1407. break;
  1408. }
  1409. }
  1410. return found;
  1411. }
  1412. /*
  1413. * Returns the size of variable_name, in bytes, including the
  1414. * terminating NULL character, or variable_name_size if no NULL
  1415. * character is found among the first variable_name_size bytes.
  1416. */
  1417. static unsigned long var_name_strnsize(efi_char16_t *variable_name,
  1418. unsigned long variable_name_size)
  1419. {
  1420. unsigned long len;
  1421. efi_char16_t c;
  1422. /*
  1423. * The variable name is, by definition, a NULL-terminated
  1424. * string, so make absolutely sure that variable_name_size is
  1425. * the value we expect it to be. If not, return the real size.
  1426. */
  1427. for (len = 2; len <= variable_name_size; len += sizeof(c)) {
  1428. c = variable_name[(len / sizeof(c)) - 1];
  1429. if (!c)
  1430. break;
  1431. }
  1432. return min(len, variable_name_size);
  1433. }
  1434. static void efivar_update_sysfs_entries(struct work_struct *work)
  1435. {
  1436. struct efivars *efivars = &__efivars;
  1437. efi_guid_t vendor;
  1438. efi_char16_t *variable_name;
  1439. unsigned long variable_name_size = 1024;
  1440. efi_status_t status = EFI_NOT_FOUND;
  1441. bool found;
  1442. /* Add new sysfs entries */
  1443. while (1) {
  1444. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1445. if (!variable_name) {
  1446. pr_err("efivars: Memory allocation failed.\n");
  1447. return;
  1448. }
  1449. spin_lock_irq(&efivars->lock);
  1450. found = false;
  1451. while (1) {
  1452. variable_name_size = 1024;
  1453. status = efivars->ops->get_next_variable(
  1454. &variable_name_size,
  1455. variable_name,
  1456. &vendor);
  1457. if (status != EFI_SUCCESS) {
  1458. break;
  1459. } else {
  1460. if (!variable_is_present(variable_name,
  1461. &vendor)) {
  1462. found = true;
  1463. break;
  1464. }
  1465. }
  1466. }
  1467. spin_unlock_irq(&efivars->lock);
  1468. if (!found) {
  1469. kfree(variable_name);
  1470. break;
  1471. } else {
  1472. variable_name_size = var_name_strnsize(variable_name,
  1473. variable_name_size);
  1474. efivar_create_sysfs_entry(efivars,
  1475. variable_name_size,
  1476. variable_name, &vendor);
  1477. }
  1478. }
  1479. }
  1480. /*
  1481. * Let's not leave out systab information that snuck into
  1482. * the efivars driver
  1483. */
  1484. static ssize_t systab_show(struct kobject *kobj,
  1485. struct kobj_attribute *attr, char *buf)
  1486. {
  1487. char *str = buf;
  1488. if (!kobj || !buf)
  1489. return -EINVAL;
  1490. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  1491. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  1492. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  1493. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  1494. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  1495. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  1496. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  1497. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  1498. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  1499. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  1500. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  1501. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  1502. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  1503. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  1504. return str - buf;
  1505. }
  1506. static struct kobj_attribute efi_attr_systab =
  1507. __ATTR(systab, 0400, systab_show, NULL);
  1508. static struct attribute *efi_subsys_attrs[] = {
  1509. &efi_attr_systab.attr,
  1510. NULL, /* maybe more in the future? */
  1511. };
  1512. static struct attribute_group efi_subsys_attr_group = {
  1513. .attrs = efi_subsys_attrs,
  1514. };
  1515. static struct kobject *efi_kobj;
  1516. /*
  1517. * efivar_create_sysfs_entry()
  1518. * Requires:
  1519. * variable_name_size = number of bytes required to hold
  1520. * variable_name (not counting the NULL
  1521. * character at the end.
  1522. * efivars->lock is not held on entry or exit.
  1523. * Returns 1 on failure, 0 on success
  1524. */
  1525. static int
  1526. efivar_create_sysfs_entry(struct efivars *efivars,
  1527. unsigned long variable_name_size,
  1528. efi_char16_t *variable_name,
  1529. efi_guid_t *vendor_guid)
  1530. {
  1531. int i, short_name_size;
  1532. char *short_name;
  1533. struct efivar_entry *new_efivar;
  1534. /*
  1535. * Length of the variable bytes in ASCII, plus the '-' separator,
  1536. * plus the GUID, plus trailing NUL
  1537. */
  1538. short_name_size = variable_name_size / sizeof(efi_char16_t)
  1539. + 1 + GUID_LEN + 1;
  1540. short_name = kzalloc(short_name_size, GFP_KERNEL);
  1541. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  1542. if (!short_name || !new_efivar) {
  1543. kfree(short_name);
  1544. kfree(new_efivar);
  1545. return 1;
  1546. }
  1547. new_efivar->efivars = efivars;
  1548. memcpy(new_efivar->var.VariableName, variable_name,
  1549. variable_name_size);
  1550. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  1551. /* Convert Unicode to normal chars (assume top bits are 0),
  1552. ala UTF-8 */
  1553. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  1554. short_name[i] = variable_name[i] & 0xFF;
  1555. }
  1556. /* This is ugly, but necessary to separate one vendor's
  1557. private variables from another's. */
  1558. *(short_name + strlen(short_name)) = '-';
  1559. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  1560. new_efivar->kobj.kset = efivars->kset;
  1561. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  1562. "%s", short_name);
  1563. if (i) {
  1564. kfree(short_name);
  1565. kfree(new_efivar);
  1566. return 1;
  1567. }
  1568. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  1569. kfree(short_name);
  1570. short_name = NULL;
  1571. spin_lock_irq(&efivars->lock);
  1572. list_add(&new_efivar->list, &efivars->list);
  1573. spin_unlock_irq(&efivars->lock);
  1574. return 0;
  1575. }
  1576. static int
  1577. create_efivars_bin_attributes(struct efivars *efivars)
  1578. {
  1579. struct bin_attribute *attr;
  1580. int error;
  1581. /* new_var */
  1582. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1583. if (!attr)
  1584. return -ENOMEM;
  1585. attr->attr.name = "new_var";
  1586. attr->attr.mode = 0200;
  1587. attr->write = efivar_create;
  1588. attr->private = efivars;
  1589. efivars->new_var = attr;
  1590. /* del_var */
  1591. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1592. if (!attr) {
  1593. error = -ENOMEM;
  1594. goto out_free;
  1595. }
  1596. attr->attr.name = "del_var";
  1597. attr->attr.mode = 0200;
  1598. attr->write = efivar_delete;
  1599. attr->private = efivars;
  1600. efivars->del_var = attr;
  1601. sysfs_bin_attr_init(efivars->new_var);
  1602. sysfs_bin_attr_init(efivars->del_var);
  1603. /* Register */
  1604. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1605. efivars->new_var);
  1606. if (error) {
  1607. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  1608. " due to error %d\n", error);
  1609. goto out_free;
  1610. }
  1611. error = sysfs_create_bin_file(&efivars->kset->kobj,
  1612. efivars->del_var);
  1613. if (error) {
  1614. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  1615. " due to error %d\n", error);
  1616. sysfs_remove_bin_file(&efivars->kset->kobj,
  1617. efivars->new_var);
  1618. goto out_free;
  1619. }
  1620. return 0;
  1621. out_free:
  1622. kfree(efivars->del_var);
  1623. efivars->del_var = NULL;
  1624. kfree(efivars->new_var);
  1625. efivars->new_var = NULL;
  1626. return error;
  1627. }
  1628. void unregister_efivars(struct efivars *efivars)
  1629. {
  1630. struct efivar_entry *entry, *n;
  1631. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  1632. spin_lock_irq(&efivars->lock);
  1633. list_del(&entry->list);
  1634. spin_unlock_irq(&efivars->lock);
  1635. efivar_unregister(entry);
  1636. }
  1637. if (efivars->new_var)
  1638. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  1639. if (efivars->del_var)
  1640. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  1641. kfree(efivars->new_var);
  1642. kfree(efivars->del_var);
  1643. kobject_put(efivars->kobject);
  1644. kset_unregister(efivars->kset);
  1645. }
  1646. EXPORT_SYMBOL_GPL(unregister_efivars);
  1647. /*
  1648. * Print a warning when duplicate EFI variables are encountered and
  1649. * disable the sysfs workqueue since the firmware is buggy.
  1650. */
  1651. static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
  1652. unsigned long len16)
  1653. {
  1654. size_t i, len8 = len16 / sizeof(efi_char16_t);
  1655. char *s8;
  1656. /*
  1657. * Disable the workqueue since the algorithm it uses for
  1658. * detecting new variables won't work with this buggy
  1659. * implementation of GetNextVariableName().
  1660. */
  1661. efivar_wq_enabled = false;
  1662. s8 = kzalloc(len8, GFP_KERNEL);
  1663. if (!s8)
  1664. return;
  1665. for (i = 0; i < len8; i++)
  1666. s8[i] = s16[i];
  1667. printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
  1668. s8, vendor_guid);
  1669. kfree(s8);
  1670. }
  1671. int register_efivars(struct efivars *efivars,
  1672. const struct efivar_operations *ops,
  1673. struct kobject *parent_kobj)
  1674. {
  1675. efi_status_t status = EFI_NOT_FOUND;
  1676. efi_guid_t vendor_guid;
  1677. efi_char16_t *variable_name;
  1678. unsigned long variable_name_size = 1024;
  1679. int error = 0;
  1680. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  1681. if (!variable_name) {
  1682. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  1683. return -ENOMEM;
  1684. }
  1685. spin_lock_init(&efivars->lock);
  1686. INIT_LIST_HEAD(&efivars->list);
  1687. efivars->ops = ops;
  1688. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  1689. if (!efivars->kset) {
  1690. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  1691. error = -ENOMEM;
  1692. goto out;
  1693. }
  1694. efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
  1695. if (!efivars->kobject) {
  1696. pr_err("efivars: Subsystem registration failed.\n");
  1697. error = -ENOMEM;
  1698. kset_unregister(efivars->kset);
  1699. goto out;
  1700. }
  1701. /*
  1702. * Per EFI spec, the maximum storage allocated for both
  1703. * the variable name and variable data is 1024 bytes.
  1704. */
  1705. do {
  1706. variable_name_size = 1024;
  1707. status = ops->get_next_variable(&variable_name_size,
  1708. variable_name,
  1709. &vendor_guid);
  1710. switch (status) {
  1711. case EFI_SUCCESS:
  1712. variable_name_size = var_name_strnsize(variable_name,
  1713. variable_name_size);
  1714. /*
  1715. * Some firmware implementations return the
  1716. * same variable name on multiple calls to
  1717. * get_next_variable(). Terminate the loop
  1718. * immediately as there is no guarantee that
  1719. * we'll ever see a different variable name,
  1720. * and may end up looping here forever.
  1721. */
  1722. if (variable_is_present(variable_name, &vendor_guid)) {
  1723. dup_variable_bug(variable_name, &vendor_guid,
  1724. variable_name_size);
  1725. status = EFI_NOT_FOUND;
  1726. break;
  1727. }
  1728. efivar_create_sysfs_entry(efivars,
  1729. variable_name_size,
  1730. variable_name,
  1731. &vendor_guid);
  1732. break;
  1733. case EFI_NOT_FOUND:
  1734. break;
  1735. default:
  1736. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1737. status);
  1738. status = EFI_NOT_FOUND;
  1739. break;
  1740. }
  1741. } while (status != EFI_NOT_FOUND);
  1742. error = create_efivars_bin_attributes(efivars);
  1743. if (error)
  1744. unregister_efivars(efivars);
  1745. if (!efivars_pstore_disable)
  1746. efivar_pstore_register(efivars);
  1747. register_filesystem(&efivarfs_type);
  1748. out:
  1749. kfree(variable_name);
  1750. return error;
  1751. }
  1752. EXPORT_SYMBOL_GPL(register_efivars);
  1753. /*
  1754. * For now we register the efi subsystem with the firmware subsystem
  1755. * and the vars subsystem with the efi subsystem. In the future, it
  1756. * might make sense to split off the efi subsystem into its own
  1757. * driver, but for now only efivars will register with it, so just
  1758. * include it here.
  1759. */
  1760. static int __init
  1761. efivars_init(void)
  1762. {
  1763. int error = 0;
  1764. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1765. EFIVARS_DATE);
  1766. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  1767. return 0;
  1768. /* For now we'll register the efi directory at /sys/firmware/efi */
  1769. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1770. if (!efi_kobj) {
  1771. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1772. return -ENOMEM;
  1773. }
  1774. ops.get_variable = efi.get_variable;
  1775. ops.set_variable = efi.set_variable;
  1776. ops.get_next_variable = efi.get_next_variable;
  1777. ops.query_variable_info = efi.query_variable_info;
  1778. error = register_efivars(&__efivars, &ops, efi_kobj);
  1779. if (error)
  1780. goto err_put;
  1781. /* Don't forget the systab entry */
  1782. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1783. if (error) {
  1784. printk(KERN_ERR
  1785. "efivars: Sysfs attribute export failed with error %d.\n",
  1786. error);
  1787. goto err_unregister;
  1788. }
  1789. return 0;
  1790. err_unregister:
  1791. unregister_efivars(&__efivars);
  1792. err_put:
  1793. kobject_put(efi_kobj);
  1794. return error;
  1795. }
  1796. static void __exit
  1797. efivars_exit(void)
  1798. {
  1799. cancel_work_sync(&efivar_work);
  1800. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  1801. unregister_efivars(&__efivars);
  1802. kobject_put(efi_kobj);
  1803. }
  1804. }
  1805. module_init(efivars_init);
  1806. module_exit(efivars_exit);