efivars.c 47 KB

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