efivars.c 52 KB

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