efivars.c 52 KB

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