efivars.c 45 KB

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