main.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * main.c - Multi purpose firmware loading support
  4. *
  5. * Copyright (c) 2003 Manuel Estrada Sainz
  6. *
  7. * Please see Documentation/driver-api/firmware/ for more information.
  8. *
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/capability.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel_read_file.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/initrd.h>
  17. #include <linux/timer.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/bitops.h>
  21. #include <linux/mutex.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/highmem.h>
  24. #include <linux/firmware.h>
  25. #include <linux/slab.h>
  26. #include <linux/sched.h>
  27. #include <linux/file.h>
  28. #include <linux/list.h>
  29. #include <linux/fs.h>
  30. #include <linux/async.h>
  31. #include <linux/pm.h>
  32. #include <linux/suspend.h>
  33. #include <linux/syscore_ops.h>
  34. #include <linux/reboot.h>
  35. #include <linux/security.h>
  36. #include <linux/zstd.h>
  37. #include <linux/xz.h>
  38. #include <generated/utsrelease.h>
  39. #include "../base.h"
  40. #include "firmware.h"
  41. #include "fallback.h"
  42. MODULE_AUTHOR("Manuel Estrada Sainz");
  43. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  44. MODULE_LICENSE("GPL");
  45. struct firmware_cache {
  46. /* firmware_buf instance will be added into the below list */
  47. spinlock_t lock;
  48. struct list_head head;
  49. int state;
  50. #ifdef CONFIG_FW_CACHE
  51. /*
  52. * Names of firmware images which have been cached successfully
  53. * will be added into the below list so that device uncache
  54. * helper can trace which firmware images have been cached
  55. * before.
  56. */
  57. spinlock_t name_lock;
  58. struct list_head fw_names;
  59. struct delayed_work work;
  60. struct notifier_block pm_notify;
  61. #endif
  62. };
  63. struct fw_cache_entry {
  64. struct list_head list;
  65. const char *name;
  66. };
  67. struct fw_name_devm {
  68. unsigned long magic;
  69. const char *name;
  70. };
  71. static inline struct fw_priv *to_fw_priv(struct kref *ref)
  72. {
  73. return container_of(ref, struct fw_priv, ref);
  74. }
  75. #define FW_LOADER_NO_CACHE 0
  76. #define FW_LOADER_START_CACHE 1
  77. /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
  78. * guarding for corner cases a global lock should be OK */
  79. DEFINE_MUTEX(fw_lock);
  80. struct firmware_cache fw_cache;
  81. bool fw_load_abort_all;
  82. void fw_state_init(struct fw_priv *fw_priv)
  83. {
  84. struct fw_state *fw_st = &fw_priv->fw_st;
  85. init_completion(&fw_st->completion);
  86. fw_st->status = FW_STATUS_UNKNOWN;
  87. }
  88. static inline int fw_state_wait(struct fw_priv *fw_priv)
  89. {
  90. return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
  91. }
  92. static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv);
  93. static struct fw_priv *__allocate_fw_priv(const char *fw_name,
  94. struct firmware_cache *fwc,
  95. void *dbuf,
  96. size_t size,
  97. size_t offset,
  98. u32 opt_flags)
  99. {
  100. struct fw_priv *fw_priv;
  101. /* For a partial read, the buffer must be preallocated. */
  102. if ((opt_flags & FW_OPT_PARTIAL) && !dbuf)
  103. return NULL;
  104. /* Only partial reads are allowed to use an offset. */
  105. if (offset != 0 && !(opt_flags & FW_OPT_PARTIAL))
  106. return NULL;
  107. fw_priv = kzalloc_obj(*fw_priv, GFP_ATOMIC);
  108. if (!fw_priv)
  109. return NULL;
  110. fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
  111. if (!fw_priv->fw_name) {
  112. kfree(fw_priv);
  113. return NULL;
  114. }
  115. kref_init(&fw_priv->ref);
  116. fw_priv->fwc = fwc;
  117. fw_priv->data = dbuf;
  118. fw_priv->allocated_size = size;
  119. fw_priv->offset = offset;
  120. fw_priv->opt_flags = opt_flags;
  121. fw_state_init(fw_priv);
  122. #ifdef CONFIG_FW_LOADER_USER_HELPER
  123. INIT_LIST_HEAD(&fw_priv->pending_list);
  124. #endif
  125. pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
  126. return fw_priv;
  127. }
  128. static struct fw_priv *__lookup_fw_priv(const char *fw_name)
  129. {
  130. struct fw_priv *tmp;
  131. struct firmware_cache *fwc = &fw_cache;
  132. list_for_each_entry(tmp, &fwc->head, list)
  133. if (!strcmp(tmp->fw_name, fw_name))
  134. return tmp;
  135. return NULL;
  136. }
  137. /* Returns 1 for batching firmware requests with the same name */
  138. int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
  139. struct fw_priv **fw_priv, void *dbuf, size_t size,
  140. size_t offset, u32 opt_flags)
  141. {
  142. struct fw_priv *tmp;
  143. spin_lock(&fwc->lock);
  144. /*
  145. * Do not merge requests that are marked to be non-cached or
  146. * are performing partial reads.
  147. */
  148. if (!(opt_flags & (FW_OPT_NOCACHE | FW_OPT_PARTIAL))) {
  149. tmp = __lookup_fw_priv(fw_name);
  150. if (tmp) {
  151. kref_get(&tmp->ref);
  152. spin_unlock(&fwc->lock);
  153. *fw_priv = tmp;
  154. pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
  155. return 1;
  156. }
  157. }
  158. tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size, offset, opt_flags);
  159. if (tmp) {
  160. INIT_LIST_HEAD(&tmp->list);
  161. if (!(opt_flags & FW_OPT_NOCACHE))
  162. list_add(&tmp->list, &fwc->head);
  163. }
  164. spin_unlock(&fwc->lock);
  165. *fw_priv = tmp;
  166. return tmp ? 0 : -ENOMEM;
  167. }
  168. static void __free_fw_priv(struct kref *ref)
  169. __releases(&fwc->lock)
  170. {
  171. struct fw_priv *fw_priv = to_fw_priv(ref);
  172. struct firmware_cache *fwc = fw_priv->fwc;
  173. pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
  174. __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
  175. (unsigned int)fw_priv->size);
  176. list_del(&fw_priv->list);
  177. spin_unlock(&fwc->lock);
  178. if (fw_is_paged_buf(fw_priv))
  179. fw_free_paged_buf(fw_priv);
  180. else if (!fw_priv->allocated_size)
  181. vfree(fw_priv->data);
  182. kfree_const(fw_priv->fw_name);
  183. kfree(fw_priv);
  184. }
  185. void free_fw_priv(struct fw_priv *fw_priv)
  186. {
  187. struct firmware_cache *fwc = fw_priv->fwc;
  188. spin_lock(&fwc->lock);
  189. if (!kref_put(&fw_priv->ref, __free_fw_priv))
  190. spin_unlock(&fwc->lock);
  191. }
  192. #ifdef CONFIG_FW_LOADER_PAGED_BUF
  193. bool fw_is_paged_buf(struct fw_priv *fw_priv)
  194. {
  195. return fw_priv->is_paged_buf;
  196. }
  197. void fw_free_paged_buf(struct fw_priv *fw_priv)
  198. {
  199. int i;
  200. if (!fw_priv->pages)
  201. return;
  202. vunmap(fw_priv->data);
  203. for (i = 0; i < fw_priv->nr_pages; i++)
  204. __free_page(fw_priv->pages[i]);
  205. kvfree(fw_priv->pages);
  206. fw_priv->pages = NULL;
  207. fw_priv->page_array_size = 0;
  208. fw_priv->nr_pages = 0;
  209. fw_priv->data = NULL;
  210. fw_priv->size = 0;
  211. }
  212. int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
  213. {
  214. /* If the array of pages is too small, grow it */
  215. if (fw_priv->page_array_size < pages_needed) {
  216. int new_array_size = max(pages_needed,
  217. fw_priv->page_array_size * 2);
  218. struct page **new_pages;
  219. new_pages = kvmalloc_array(new_array_size, sizeof(void *),
  220. GFP_KERNEL);
  221. if (!new_pages)
  222. return -ENOMEM;
  223. memcpy(new_pages, fw_priv->pages,
  224. fw_priv->page_array_size * sizeof(void *));
  225. memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
  226. (new_array_size - fw_priv->page_array_size));
  227. kvfree(fw_priv->pages);
  228. fw_priv->pages = new_pages;
  229. fw_priv->page_array_size = new_array_size;
  230. }
  231. while (fw_priv->nr_pages < pages_needed) {
  232. fw_priv->pages[fw_priv->nr_pages] =
  233. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  234. if (!fw_priv->pages[fw_priv->nr_pages])
  235. return -ENOMEM;
  236. fw_priv->nr_pages++;
  237. }
  238. return 0;
  239. }
  240. int fw_map_paged_buf(struct fw_priv *fw_priv)
  241. {
  242. /* one pages buffer should be mapped/unmapped only once */
  243. if (!fw_priv->pages)
  244. return 0;
  245. vunmap(fw_priv->data);
  246. fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
  247. PAGE_KERNEL_RO);
  248. if (!fw_priv->data)
  249. return -ENOMEM;
  250. return 0;
  251. }
  252. #endif
  253. /*
  254. * ZSTD-compressed firmware support
  255. */
  256. #ifdef CONFIG_FW_LOADER_COMPRESS_ZSTD
  257. static int fw_decompress_zstd(struct device *dev, struct fw_priv *fw_priv,
  258. size_t in_size, const void *in_buffer)
  259. {
  260. size_t len, out_size, workspace_size;
  261. void *workspace, *out_buf;
  262. zstd_dctx *ctx;
  263. int err;
  264. if (fw_priv->allocated_size) {
  265. out_size = fw_priv->allocated_size;
  266. out_buf = fw_priv->data;
  267. } else {
  268. zstd_frame_header params;
  269. if (zstd_get_frame_header(&params, in_buffer, in_size) ||
  270. params.frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN) {
  271. dev_dbg(dev, "%s: invalid zstd header\n", __func__);
  272. return -EINVAL;
  273. }
  274. out_size = params.frameContentSize;
  275. out_buf = vzalloc(out_size);
  276. if (!out_buf)
  277. return -ENOMEM;
  278. }
  279. workspace_size = zstd_dctx_workspace_bound();
  280. workspace = kvzalloc(workspace_size, GFP_KERNEL);
  281. if (!workspace) {
  282. err = -ENOMEM;
  283. goto error;
  284. }
  285. ctx = zstd_init_dctx(workspace, workspace_size);
  286. if (!ctx) {
  287. dev_dbg(dev, "%s: failed to initialize context\n", __func__);
  288. err = -EINVAL;
  289. goto error;
  290. }
  291. len = zstd_decompress_dctx(ctx, out_buf, out_size, in_buffer, in_size);
  292. if (zstd_is_error(len)) {
  293. dev_dbg(dev, "%s: failed to decompress: %d\n", __func__,
  294. zstd_get_error_code(len));
  295. err = -EINVAL;
  296. goto error;
  297. }
  298. if (!fw_priv->allocated_size)
  299. fw_priv->data = out_buf;
  300. fw_priv->size = len;
  301. err = 0;
  302. error:
  303. kvfree(workspace);
  304. if (err && !fw_priv->allocated_size)
  305. vfree(out_buf);
  306. return err;
  307. }
  308. #endif /* CONFIG_FW_LOADER_COMPRESS_ZSTD */
  309. /*
  310. * XZ-compressed firmware support
  311. */
  312. #ifdef CONFIG_FW_LOADER_COMPRESS_XZ
  313. /* show an error and return the standard error code */
  314. static int fw_decompress_xz_error(struct device *dev, enum xz_ret xz_ret)
  315. {
  316. if (xz_ret != XZ_STREAM_END) {
  317. dev_warn(dev, "xz decompression failed (xz_ret=%d)\n", xz_ret);
  318. return xz_ret == XZ_MEM_ERROR ? -ENOMEM : -EINVAL;
  319. }
  320. return 0;
  321. }
  322. /* single-shot decompression onto the pre-allocated buffer */
  323. static int fw_decompress_xz_single(struct device *dev, struct fw_priv *fw_priv,
  324. size_t in_size, const void *in_buffer)
  325. {
  326. struct xz_dec *xz_dec;
  327. struct xz_buf xz_buf;
  328. enum xz_ret xz_ret;
  329. xz_dec = xz_dec_init(XZ_SINGLE, (u32)-1);
  330. if (!xz_dec)
  331. return -ENOMEM;
  332. xz_buf.in_size = in_size;
  333. xz_buf.in = in_buffer;
  334. xz_buf.in_pos = 0;
  335. xz_buf.out_size = fw_priv->allocated_size;
  336. xz_buf.out = fw_priv->data;
  337. xz_buf.out_pos = 0;
  338. xz_ret = xz_dec_run(xz_dec, &xz_buf);
  339. xz_dec_end(xz_dec);
  340. fw_priv->size = xz_buf.out_pos;
  341. return fw_decompress_xz_error(dev, xz_ret);
  342. }
  343. /* decompression on paged buffer and map it */
  344. static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv,
  345. size_t in_size, const void *in_buffer)
  346. {
  347. struct xz_dec *xz_dec;
  348. struct xz_buf xz_buf;
  349. enum xz_ret xz_ret;
  350. struct page *page;
  351. int err = 0;
  352. xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
  353. if (!xz_dec)
  354. return -ENOMEM;
  355. xz_buf.in_size = in_size;
  356. xz_buf.in = in_buffer;
  357. xz_buf.in_pos = 0;
  358. fw_priv->is_paged_buf = true;
  359. fw_priv->size = 0;
  360. do {
  361. if (fw_grow_paged_buf(fw_priv, fw_priv->nr_pages + 1)) {
  362. err = -ENOMEM;
  363. goto out;
  364. }
  365. /* decompress onto the new allocated page */
  366. page = fw_priv->pages[fw_priv->nr_pages - 1];
  367. xz_buf.out = kmap_local_page(page);
  368. xz_buf.out_pos = 0;
  369. xz_buf.out_size = PAGE_SIZE;
  370. xz_ret = xz_dec_run(xz_dec, &xz_buf);
  371. kunmap_local(xz_buf.out);
  372. fw_priv->size += xz_buf.out_pos;
  373. /* partial decompression means either end or error */
  374. if (xz_buf.out_pos != PAGE_SIZE)
  375. break;
  376. } while (xz_ret == XZ_OK);
  377. err = fw_decompress_xz_error(dev, xz_ret);
  378. if (!err)
  379. err = fw_map_paged_buf(fw_priv);
  380. out:
  381. xz_dec_end(xz_dec);
  382. return err;
  383. }
  384. static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
  385. size_t in_size, const void *in_buffer)
  386. {
  387. /* if the buffer is pre-allocated, we can perform in single-shot mode */
  388. if (fw_priv->data)
  389. return fw_decompress_xz_single(dev, fw_priv, in_size, in_buffer);
  390. else
  391. return fw_decompress_xz_pages(dev, fw_priv, in_size, in_buffer);
  392. }
  393. #endif /* CONFIG_FW_LOADER_COMPRESS_XZ */
  394. /* direct firmware loading support */
  395. static char fw_path_para[256];
  396. static const char * const fw_path[] = {
  397. fw_path_para,
  398. "/lib/firmware/updates/" UTS_RELEASE,
  399. "/lib/firmware/updates",
  400. "/lib/firmware/" UTS_RELEASE,
  401. "/lib/firmware"
  402. };
  403. /*
  404. * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
  405. * from kernel command line because firmware_class is generally built in
  406. * kernel instead of module.
  407. */
  408. module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
  409. MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
  410. static int
  411. fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
  412. const char *suffix,
  413. int (*decompress)(struct device *dev,
  414. struct fw_priv *fw_priv,
  415. size_t in_size,
  416. const void *in_buffer))
  417. {
  418. size_t size;
  419. int i, len, maxlen = 0;
  420. int rc = -ENOENT;
  421. char *path, *nt = NULL;
  422. size_t msize = INT_MAX;
  423. void *buffer = NULL;
  424. /* Already populated data member means we're loading into a buffer */
  425. if (!decompress && fw_priv->data) {
  426. buffer = fw_priv->data;
  427. msize = fw_priv->allocated_size;
  428. }
  429. path = __getname();
  430. if (!path)
  431. return -ENOMEM;
  432. wait_for_initramfs();
  433. for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
  434. size_t file_size = 0;
  435. size_t *file_size_ptr = NULL;
  436. /* skip the unset customized path */
  437. if (!fw_path[i][0])
  438. continue;
  439. /* strip off \n from customized path */
  440. maxlen = strlen(fw_path[i]);
  441. if (i == 0) {
  442. nt = strchr(fw_path[i], '\n');
  443. if (nt)
  444. maxlen = nt - fw_path[i];
  445. }
  446. len = snprintf(path, PATH_MAX, "%.*s/%s%s",
  447. maxlen, fw_path[i],
  448. fw_priv->fw_name, suffix);
  449. if (len >= PATH_MAX) {
  450. rc = -ENAMETOOLONG;
  451. break;
  452. }
  453. fw_priv->size = 0;
  454. /*
  455. * The total file size is only examined when doing a partial
  456. * read; the "full read" case needs to fail if the whole
  457. * firmware was not completely loaded.
  458. */
  459. if ((fw_priv->opt_flags & FW_OPT_PARTIAL) && buffer)
  460. file_size_ptr = &file_size;
  461. /* load firmware files from the mount namespace of init */
  462. rc = kernel_read_file_from_path_initns(path, fw_priv->offset,
  463. &buffer, msize,
  464. file_size_ptr,
  465. READING_FIRMWARE);
  466. if (rc < 0) {
  467. if (!(fw_priv->opt_flags & FW_OPT_NO_WARN)) {
  468. if (rc != -ENOENT)
  469. dev_warn(device,
  470. "loading %s failed with error %d\n",
  471. path, rc);
  472. else
  473. dev_dbg(device,
  474. "loading %s failed for no such file or directory.\n",
  475. path);
  476. }
  477. continue;
  478. }
  479. size = rc;
  480. rc = 0;
  481. dev_dbg(device, "Loading firmware from %s\n", path);
  482. if (decompress) {
  483. dev_dbg(device, "f/w decompressing %s\n",
  484. fw_priv->fw_name);
  485. rc = decompress(device, fw_priv, size, buffer);
  486. /* discard the superfluous original content */
  487. vfree(buffer);
  488. buffer = NULL;
  489. if (rc) {
  490. fw_free_paged_buf(fw_priv);
  491. continue;
  492. }
  493. } else {
  494. dev_dbg(device, "direct-loading %s\n",
  495. fw_priv->fw_name);
  496. if (!fw_priv->data)
  497. fw_priv->data = buffer;
  498. fw_priv->size = size;
  499. }
  500. fw_state_done(fw_priv);
  501. break;
  502. }
  503. __putname(path);
  504. return rc;
  505. }
  506. /* firmware holds the ownership of pages */
  507. static void firmware_free_data(const struct firmware *fw)
  508. {
  509. /* Loaded directly? */
  510. if (!fw->priv) {
  511. vfree(fw->data);
  512. return;
  513. }
  514. free_fw_priv(fw->priv);
  515. }
  516. /* store the pages buffer info firmware from buf */
  517. static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
  518. {
  519. fw->priv = fw_priv;
  520. fw->size = fw_priv->size;
  521. fw->data = fw_priv->data;
  522. pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
  523. __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
  524. (unsigned int)fw_priv->size);
  525. }
  526. #ifdef CONFIG_FW_CACHE
  527. static void fw_name_devm_release(struct device *dev, void *res)
  528. {
  529. struct fw_name_devm *fwn = res;
  530. if (fwn->magic == (unsigned long)&fw_cache)
  531. pr_debug("%s: fw_name-%s devm-%p released\n",
  532. __func__, fwn->name, res);
  533. kfree_const(fwn->name);
  534. }
  535. static int fw_devm_match(struct device *dev, void *res,
  536. void *match_data)
  537. {
  538. struct fw_name_devm *fwn = res;
  539. return (fwn->magic == (unsigned long)&fw_cache) &&
  540. !strcmp(fwn->name, match_data);
  541. }
  542. static struct fw_name_devm *fw_find_devm_name(struct device *dev,
  543. const char *name)
  544. {
  545. struct fw_name_devm *fwn;
  546. fwn = devres_find(dev, fw_name_devm_release,
  547. fw_devm_match, (void *)name);
  548. return fwn;
  549. }
  550. static bool fw_cache_is_setup(struct device *dev, const char *name)
  551. {
  552. struct fw_name_devm *fwn;
  553. fwn = fw_find_devm_name(dev, name);
  554. if (fwn)
  555. return true;
  556. return false;
  557. }
  558. /* add firmware name into devres list */
  559. static int fw_add_devm_name(struct device *dev, const char *name)
  560. {
  561. struct fw_name_devm *fwn;
  562. if (fw_cache_is_setup(dev, name))
  563. return 0;
  564. fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
  565. GFP_KERNEL);
  566. if (!fwn)
  567. return -ENOMEM;
  568. fwn->name = kstrdup_const(name, GFP_KERNEL);
  569. if (!fwn->name) {
  570. devres_free(fwn);
  571. return -ENOMEM;
  572. }
  573. fwn->magic = (unsigned long)&fw_cache;
  574. devres_add(dev, fwn);
  575. return 0;
  576. }
  577. #else
  578. static bool fw_cache_is_setup(struct device *dev, const char *name)
  579. {
  580. return false;
  581. }
  582. static int fw_add_devm_name(struct device *dev, const char *name)
  583. {
  584. return 0;
  585. }
  586. #endif
  587. int assign_fw(struct firmware *fw, struct device *device)
  588. {
  589. struct fw_priv *fw_priv = fw->priv;
  590. int ret;
  591. mutex_lock(&fw_lock);
  592. if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
  593. mutex_unlock(&fw_lock);
  594. return -ENOENT;
  595. }
  596. /*
  597. * add firmware name into devres list so that we can auto cache
  598. * and uncache firmware for device.
  599. *
  600. * device may has been deleted already, but the problem
  601. * should be fixed in devres or driver core.
  602. */
  603. /* don't cache firmware handled without uevent */
  604. if (device && (fw_priv->opt_flags & FW_OPT_UEVENT) &&
  605. !(fw_priv->opt_flags & FW_OPT_NOCACHE)) {
  606. ret = fw_add_devm_name(device, fw_priv->fw_name);
  607. if (ret) {
  608. mutex_unlock(&fw_lock);
  609. return ret;
  610. }
  611. }
  612. /*
  613. * After caching firmware image is started, let it piggyback
  614. * on request firmware.
  615. */
  616. if (!(fw_priv->opt_flags & FW_OPT_NOCACHE) &&
  617. fw_priv->fwc->state == FW_LOADER_START_CACHE)
  618. fw_cache_piggyback_on_request(fw_priv);
  619. /* pass the pages buffer to driver at the last minute */
  620. fw_set_page_data(fw_priv, fw);
  621. mutex_unlock(&fw_lock);
  622. return 0;
  623. }
  624. /* prepare firmware and firmware_buf structs;
  625. * return 0 if a firmware is already assigned, 1 if need to load one,
  626. * or a negative error code
  627. */
  628. static int
  629. _request_firmware_prepare(struct firmware **firmware_p, const char *name,
  630. struct device *device, void *dbuf, size_t size,
  631. size_t offset, u32 opt_flags)
  632. {
  633. struct firmware *firmware;
  634. struct fw_priv *fw_priv;
  635. int ret;
  636. *firmware_p = firmware = kzalloc_obj(*firmware);
  637. if (!firmware) {
  638. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  639. __func__);
  640. return -ENOMEM;
  641. }
  642. if (firmware_request_builtin_buf(firmware, name, dbuf, size)) {
  643. dev_dbg(device, "using built-in %s\n", name);
  644. return 0; /* assigned */
  645. }
  646. ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
  647. offset, opt_flags);
  648. /*
  649. * bind with 'priv' now to avoid warning in failure path
  650. * of requesting firmware.
  651. */
  652. firmware->priv = fw_priv;
  653. if (ret > 0) {
  654. ret = fw_state_wait(fw_priv);
  655. if (!ret) {
  656. fw_set_page_data(fw_priv, firmware);
  657. return 0; /* assigned */
  658. }
  659. }
  660. if (ret < 0)
  661. return ret;
  662. return 1; /* need to load */
  663. }
  664. /*
  665. * Batched requests need only one wake, we need to do this step last due to the
  666. * fallback mechanism. The buf is protected with kref_get(), and it won't be
  667. * released until the last user calls release_firmware().
  668. *
  669. * Failed batched requests are possible as well, in such cases we just share
  670. * the struct fw_priv and won't release it until all requests are woken
  671. * and have gone through this same path.
  672. */
  673. static void fw_abort_batch_reqs(struct firmware *fw)
  674. {
  675. struct fw_priv *fw_priv;
  676. /* Loaded directly? */
  677. if (!fw || !fw->priv)
  678. return;
  679. fw_priv = fw->priv;
  680. mutex_lock(&fw_lock);
  681. if (!fw_state_is_aborted(fw_priv))
  682. fw_state_aborted(fw_priv);
  683. mutex_unlock(&fw_lock);
  684. }
  685. #if defined(CONFIG_FW_LOADER_DEBUG)
  686. #include <crypto/sha2.h>
  687. static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
  688. {
  689. u8 digest[SHA256_DIGEST_SIZE];
  690. sha256(fw->data, fw->size, digest);
  691. dev_dbg(device, "Loaded FW: %s, sha256: %*phN\n",
  692. name, SHA256_DIGEST_SIZE, digest);
  693. }
  694. #else
  695. static void fw_log_firmware_info(const struct firmware *fw, const char *name,
  696. struct device *device)
  697. {}
  698. #endif
  699. /* called from request_firmware() and request_firmware_work_func() */
  700. static int
  701. _request_firmware(const struct firmware **firmware_p, const char *name,
  702. struct device *device, void *buf, size_t size,
  703. size_t offset, u32 opt_flags)
  704. {
  705. struct firmware *fw = NULL;
  706. bool nondirect = false;
  707. int ret;
  708. if (!firmware_p)
  709. return -EINVAL;
  710. if (!name || name[0] == '\0') {
  711. ret = -EINVAL;
  712. goto out;
  713. }
  714. /*
  715. * Reject firmware file names with ".." path components.
  716. * There are drivers that construct firmware file names from
  717. * device-supplied strings, and we don't want some device to be
  718. * able to tell us "I would like to be sent my firmware from
  719. * ../../../etc/shadow, please".
  720. *
  721. * This intentionally only looks at the firmware name, not at
  722. * the firmware base directory or at symlink contents.
  723. */
  724. if (name_contains_dotdot(name)) {
  725. dev_warn(device,
  726. "Firmware load for '%s' refused, path contains '..' component\n",
  727. name);
  728. ret = -EINVAL;
  729. goto out;
  730. }
  731. ret = _request_firmware_prepare(&fw, name, device, buf, size,
  732. offset, opt_flags);
  733. if (ret <= 0) /* error or already assigned */
  734. goto out;
  735. /*
  736. * We are about to try to access the firmware file. Because we may have been
  737. * called by a driver when serving an unrelated request from userland, we use
  738. * the kernel credentials to read the file.
  739. */
  740. scoped_with_kernel_creds() {
  741. ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
  742. /* Only full reads can support decompression, platform, and sysfs. */
  743. if (!(opt_flags & FW_OPT_PARTIAL))
  744. nondirect = true;
  745. #ifdef CONFIG_FW_LOADER_COMPRESS_ZSTD
  746. if (ret == -ENOENT && nondirect)
  747. ret = fw_get_filesystem_firmware(device, fw->priv, ".zst",
  748. fw_decompress_zstd);
  749. #endif
  750. #ifdef CONFIG_FW_LOADER_COMPRESS_XZ
  751. if (ret == -ENOENT && nondirect)
  752. ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
  753. fw_decompress_xz);
  754. #endif
  755. if (ret == -ENOENT && nondirect)
  756. ret = firmware_fallback_platform(fw->priv);
  757. if (ret) {
  758. if (!(opt_flags & FW_OPT_NO_WARN))
  759. dev_warn(device,
  760. "Direct firmware load for %s failed with error %d\n",
  761. name, ret);
  762. if (nondirect)
  763. ret = firmware_fallback_sysfs(fw, name, device,
  764. opt_flags, ret);
  765. } else {
  766. ret = assign_fw(fw, device);
  767. }
  768. }
  769. out:
  770. if (ret < 0) {
  771. fw_abort_batch_reqs(fw);
  772. release_firmware(fw);
  773. fw = NULL;
  774. } else {
  775. fw_log_firmware_info(fw, name, device);
  776. }
  777. *firmware_p = fw;
  778. return ret;
  779. }
  780. /**
  781. * request_firmware() - send firmware request and wait for it
  782. * @firmware_p: pointer to firmware image
  783. * @name: name of firmware file
  784. * @device: device for which firmware is being loaded
  785. *
  786. * @firmware_p will be used to return a firmware image by the name
  787. * of @name for device @device.
  788. *
  789. * Should be called from user context where sleeping is allowed.
  790. *
  791. * @name will be used as $FIRMWARE in the uevent environment and
  792. * should be distinctive enough not to be confused with any other
  793. * firmware image for this or any other device.
  794. * It must not contain any ".." path components - "foo/bar..bin" is
  795. * allowed, but "foo/../bar.bin" is not.
  796. *
  797. * Caller must hold the reference count of @device.
  798. *
  799. * The function can be called safely inside device's suspend and
  800. * resume callback.
  801. **/
  802. int
  803. request_firmware(const struct firmware **firmware_p, const char *name,
  804. struct device *device)
  805. {
  806. int ret;
  807. /* Need to pin this module until return */
  808. __module_get(THIS_MODULE);
  809. ret = _request_firmware(firmware_p, name, device, NULL, 0, 0,
  810. FW_OPT_UEVENT);
  811. module_put(THIS_MODULE);
  812. return ret;
  813. }
  814. EXPORT_SYMBOL(request_firmware);
  815. /**
  816. * firmware_request_nowarn() - request for an optional fw module
  817. * @firmware: pointer to firmware image
  818. * @name: name of firmware file
  819. * @device: device for which firmware is being loaded
  820. *
  821. * This function is similar in behaviour to request_firmware(), except it
  822. * doesn't produce warning messages when the file is not found. The sysfs
  823. * fallback mechanism is enabled if direct filesystem lookup fails. However,
  824. * failures to find the firmware file with it are still suppressed. It is
  825. * therefore up to the driver to check for the return value of this call and to
  826. * decide when to inform the users of errors.
  827. **/
  828. int firmware_request_nowarn(const struct firmware **firmware, const char *name,
  829. struct device *device)
  830. {
  831. int ret;
  832. /* Need to pin this module until return */
  833. __module_get(THIS_MODULE);
  834. ret = _request_firmware(firmware, name, device, NULL, 0, 0,
  835. FW_OPT_UEVENT | FW_OPT_NO_WARN);
  836. module_put(THIS_MODULE);
  837. return ret;
  838. }
  839. EXPORT_SYMBOL_GPL(firmware_request_nowarn);
  840. /**
  841. * request_firmware_direct() - load firmware directly without usermode helper
  842. * @firmware_p: pointer to firmware image
  843. * @name: name of firmware file
  844. * @device: device for which firmware is being loaded
  845. *
  846. * This function works pretty much like request_firmware(), but this doesn't
  847. * fall back to usermode helper even if the firmware couldn't be loaded
  848. * directly from fs. Hence it's useful for loading optional firmwares, which
  849. * aren't always present, without extra long timeouts of udev.
  850. **/
  851. int request_firmware_direct(const struct firmware **firmware_p,
  852. const char *name, struct device *device)
  853. {
  854. int ret;
  855. __module_get(THIS_MODULE);
  856. ret = _request_firmware(firmware_p, name, device, NULL, 0, 0,
  857. FW_OPT_UEVENT | FW_OPT_NO_WARN |
  858. FW_OPT_NOFALLBACK_SYSFS);
  859. module_put(THIS_MODULE);
  860. return ret;
  861. }
  862. EXPORT_SYMBOL_GPL(request_firmware_direct);
  863. /**
  864. * firmware_request_platform() - request firmware with platform-fw fallback
  865. * @firmware: pointer to firmware image
  866. * @name: name of firmware file
  867. * @device: device for which firmware is being loaded
  868. *
  869. * This function is similar in behaviour to request_firmware, except that if
  870. * direct filesystem lookup fails, it will fallback to looking for a copy of the
  871. * requested firmware embedded in the platform's main (e.g. UEFI) firmware.
  872. **/
  873. int firmware_request_platform(const struct firmware **firmware,
  874. const char *name, struct device *device)
  875. {
  876. int ret;
  877. /* Need to pin this module until return */
  878. __module_get(THIS_MODULE);
  879. ret = _request_firmware(firmware, name, device, NULL, 0, 0,
  880. FW_OPT_UEVENT | FW_OPT_FALLBACK_PLATFORM);
  881. module_put(THIS_MODULE);
  882. return ret;
  883. }
  884. EXPORT_SYMBOL_GPL(firmware_request_platform);
  885. /**
  886. * firmware_request_cache() - cache firmware for suspend so resume can use it
  887. * @device: device for which firmware should be cached for
  888. * @name: name of firmware file
  889. *
  890. * There are some devices with an optimization that enables the device to not
  891. * require loading firmware on system reboot. This optimization may still
  892. * require the firmware present on resume from suspend. This routine can be
  893. * used to ensure the firmware is present on resume from suspend in these
  894. * situations. This helper is not compatible with drivers which use
  895. * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
  896. **/
  897. int firmware_request_cache(struct device *device, const char *name)
  898. {
  899. int ret;
  900. mutex_lock(&fw_lock);
  901. ret = fw_add_devm_name(device, name);
  902. mutex_unlock(&fw_lock);
  903. return ret;
  904. }
  905. EXPORT_SYMBOL_GPL(firmware_request_cache);
  906. /**
  907. * request_firmware_into_buf() - load firmware into a previously allocated buffer
  908. * @firmware_p: pointer to firmware image
  909. * @name: name of firmware file
  910. * @device: device for which firmware is being loaded and DMA region allocated
  911. * @buf: address of buffer to load firmware into
  912. * @size: size of buffer
  913. *
  914. * This function works pretty much like request_firmware(), but it doesn't
  915. * allocate a buffer to hold the firmware data. Instead, the firmware
  916. * is loaded directly into the buffer pointed to by @buf and the @firmware_p
  917. * data member is pointed at @buf.
  918. *
  919. * This function doesn't cache firmware either.
  920. */
  921. int
  922. request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
  923. struct device *device, void *buf, size_t size)
  924. {
  925. int ret;
  926. if (fw_cache_is_setup(device, name))
  927. return -EOPNOTSUPP;
  928. __module_get(THIS_MODULE);
  929. ret = _request_firmware(firmware_p, name, device, buf, size, 0,
  930. FW_OPT_UEVENT | FW_OPT_NOCACHE);
  931. module_put(THIS_MODULE);
  932. return ret;
  933. }
  934. EXPORT_SYMBOL(request_firmware_into_buf);
  935. /**
  936. * request_partial_firmware_into_buf() - load partial firmware into a previously allocated buffer
  937. * @firmware_p: pointer to firmware image
  938. * @name: name of firmware file
  939. * @device: device for which firmware is being loaded and DMA region allocated
  940. * @buf: address of buffer to load firmware into
  941. * @size: size of buffer
  942. * @offset: offset into file to read
  943. *
  944. * This function works pretty much like request_firmware_into_buf except
  945. * it allows a partial read of the file.
  946. */
  947. int
  948. request_partial_firmware_into_buf(const struct firmware **firmware_p,
  949. const char *name, struct device *device,
  950. void *buf, size_t size, size_t offset)
  951. {
  952. int ret;
  953. if (fw_cache_is_setup(device, name))
  954. return -EOPNOTSUPP;
  955. __module_get(THIS_MODULE);
  956. ret = _request_firmware(firmware_p, name, device, buf, size, offset,
  957. FW_OPT_UEVENT | FW_OPT_NOCACHE |
  958. FW_OPT_PARTIAL);
  959. module_put(THIS_MODULE);
  960. return ret;
  961. }
  962. EXPORT_SYMBOL(request_partial_firmware_into_buf);
  963. /**
  964. * release_firmware() - release the resource associated with a firmware image
  965. * @fw: firmware resource to release
  966. **/
  967. void release_firmware(const struct firmware *fw)
  968. {
  969. if (fw) {
  970. if (!firmware_is_builtin(fw))
  971. firmware_free_data(fw);
  972. kfree(fw);
  973. }
  974. }
  975. EXPORT_SYMBOL(release_firmware);
  976. /* Async support */
  977. struct firmware_work {
  978. struct work_struct work;
  979. struct module *module;
  980. const char *name;
  981. struct device *device;
  982. void *context;
  983. void (*cont)(const struct firmware *fw, void *context);
  984. u32 opt_flags;
  985. };
  986. static void request_firmware_work_func(struct work_struct *work)
  987. {
  988. struct firmware_work *fw_work;
  989. const struct firmware *fw;
  990. fw_work = container_of(work, struct firmware_work, work);
  991. _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0, 0,
  992. fw_work->opt_flags);
  993. fw_work->cont(fw, fw_work->context);
  994. put_device(fw_work->device); /* taken in request_firmware_nowait() */
  995. module_put(fw_work->module);
  996. kfree_const(fw_work->name);
  997. kfree(fw_work);
  998. }
  999. static int _request_firmware_nowait(
  1000. struct module *module, bool uevent,
  1001. const char *name, struct device *device, gfp_t gfp, void *context,
  1002. void (*cont)(const struct firmware *fw, void *context), bool nowarn)
  1003. {
  1004. struct firmware_work *fw_work;
  1005. fw_work = kzalloc_obj(struct firmware_work, gfp);
  1006. if (!fw_work)
  1007. return -ENOMEM;
  1008. fw_work->module = module;
  1009. fw_work->name = kstrdup_const(name, gfp);
  1010. if (!fw_work->name) {
  1011. kfree(fw_work);
  1012. return -ENOMEM;
  1013. }
  1014. fw_work->device = device;
  1015. fw_work->context = context;
  1016. fw_work->cont = cont;
  1017. fw_work->opt_flags = FW_OPT_NOWAIT |
  1018. (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER) |
  1019. (nowarn ? FW_OPT_NO_WARN : 0);
  1020. if (!uevent && fw_cache_is_setup(device, name)) {
  1021. kfree_const(fw_work->name);
  1022. kfree(fw_work);
  1023. return -EOPNOTSUPP;
  1024. }
  1025. if (!try_module_get(module)) {
  1026. kfree_const(fw_work->name);
  1027. kfree(fw_work);
  1028. return -EFAULT;
  1029. }
  1030. get_device(fw_work->device);
  1031. INIT_WORK(&fw_work->work, request_firmware_work_func);
  1032. schedule_work(&fw_work->work);
  1033. return 0;
  1034. }
  1035. /**
  1036. * request_firmware_nowait() - asynchronous version of request_firmware
  1037. * @module: module requesting the firmware
  1038. * @uevent: sends uevent to copy the firmware image if this flag
  1039. * is non-zero else the firmware copy must be done manually.
  1040. * @name: name of firmware file
  1041. * @device: device for which firmware is being loaded
  1042. * @gfp: allocation flags
  1043. * @context: will be passed over to @cont, and
  1044. * @fw may be %NULL if firmware request fails.
  1045. * @cont: function will be called asynchronously when the firmware
  1046. * request is over.
  1047. *
  1048. * Caller must hold the reference count of @device.
  1049. *
  1050. * Asynchronous variant of request_firmware() for user contexts:
  1051. * - sleep for as small periods as possible since it may
  1052. * increase kernel boot time of built-in device drivers
  1053. * requesting firmware in their ->probe() methods, if
  1054. * @gfp is GFP_KERNEL.
  1055. *
  1056. * - can't sleep at all if @gfp is GFP_ATOMIC.
  1057. **/
  1058. int request_firmware_nowait(
  1059. struct module *module, bool uevent,
  1060. const char *name, struct device *device, gfp_t gfp, void *context,
  1061. void (*cont)(const struct firmware *fw, void *context))
  1062. {
  1063. return _request_firmware_nowait(module, uevent, name, device, gfp,
  1064. context, cont, false);
  1065. }
  1066. EXPORT_SYMBOL(request_firmware_nowait);
  1067. /**
  1068. * firmware_request_nowait_nowarn() - async version of request_firmware_nowarn
  1069. * @module: module requesting the firmware
  1070. * @name: name of firmware file
  1071. * @device: device for which firmware is being loaded
  1072. * @gfp: allocation flags
  1073. * @context: will be passed over to @cont, and
  1074. * @fw may be %NULL if firmware request fails.
  1075. * @cont: function will be called asynchronously when the firmware
  1076. * request is over.
  1077. *
  1078. * Similar in function to request_firmware_nowait(), but doesn't print a warning
  1079. * when the firmware file could not be found and always sends a uevent to copy
  1080. * the firmware image.
  1081. */
  1082. int firmware_request_nowait_nowarn(
  1083. struct module *module, const char *name,
  1084. struct device *device, gfp_t gfp, void *context,
  1085. void (*cont)(const struct firmware *fw, void *context))
  1086. {
  1087. return _request_firmware_nowait(module, FW_ACTION_UEVENT, name, device,
  1088. gfp, context, cont, true);
  1089. }
  1090. EXPORT_SYMBOL_GPL(firmware_request_nowait_nowarn);
  1091. #ifdef CONFIG_FW_CACHE
  1092. static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
  1093. /**
  1094. * cache_firmware() - cache one firmware image in kernel memory space
  1095. * @fw_name: the firmware image name
  1096. *
  1097. * Cache firmware in kernel memory so that drivers can use it when
  1098. * system isn't ready for them to request firmware image from userspace.
  1099. * Once it returns successfully, driver can use request_firmware or its
  1100. * nowait version to get the cached firmware without any interacting
  1101. * with userspace
  1102. *
  1103. * Return 0 if the firmware image has been cached successfully
  1104. * Return !0 otherwise
  1105. *
  1106. */
  1107. static int cache_firmware(const char *fw_name)
  1108. {
  1109. int ret;
  1110. const struct firmware *fw;
  1111. pr_debug("%s: %s\n", __func__, fw_name);
  1112. ret = request_firmware(&fw, fw_name, NULL);
  1113. if (!ret)
  1114. kfree(fw);
  1115. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  1116. return ret;
  1117. }
  1118. static struct fw_priv *lookup_fw_priv(const char *fw_name)
  1119. {
  1120. struct fw_priv *tmp;
  1121. struct firmware_cache *fwc = &fw_cache;
  1122. spin_lock(&fwc->lock);
  1123. tmp = __lookup_fw_priv(fw_name);
  1124. spin_unlock(&fwc->lock);
  1125. return tmp;
  1126. }
  1127. /**
  1128. * uncache_firmware() - remove one cached firmware image
  1129. * @fw_name: the firmware image name
  1130. *
  1131. * Uncache one firmware image which has been cached successfully
  1132. * before.
  1133. *
  1134. * Return 0 if the firmware cache has been removed successfully
  1135. * Return !0 otherwise
  1136. *
  1137. */
  1138. static int uncache_firmware(const char *fw_name)
  1139. {
  1140. struct fw_priv *fw_priv;
  1141. struct firmware fw;
  1142. pr_debug("%s: %s\n", __func__, fw_name);
  1143. if (firmware_request_builtin(&fw, fw_name))
  1144. return 0;
  1145. fw_priv = lookup_fw_priv(fw_name);
  1146. if (fw_priv) {
  1147. free_fw_priv(fw_priv);
  1148. return 0;
  1149. }
  1150. return -EINVAL;
  1151. }
  1152. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  1153. {
  1154. struct fw_cache_entry *fce;
  1155. fce = kzalloc_obj(*fce, GFP_ATOMIC);
  1156. if (!fce)
  1157. goto exit;
  1158. fce->name = kstrdup_const(name, GFP_ATOMIC);
  1159. if (!fce->name) {
  1160. kfree(fce);
  1161. fce = NULL;
  1162. goto exit;
  1163. }
  1164. exit:
  1165. return fce;
  1166. }
  1167. static int __fw_entry_found(const char *name)
  1168. {
  1169. struct firmware_cache *fwc = &fw_cache;
  1170. struct fw_cache_entry *fce;
  1171. list_for_each_entry(fce, &fwc->fw_names, list) {
  1172. if (!strcmp(fce->name, name))
  1173. return 1;
  1174. }
  1175. return 0;
  1176. }
  1177. static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
  1178. {
  1179. const char *name = fw_priv->fw_name;
  1180. struct firmware_cache *fwc = fw_priv->fwc;
  1181. struct fw_cache_entry *fce;
  1182. spin_lock(&fwc->name_lock);
  1183. if (__fw_entry_found(name))
  1184. goto found;
  1185. fce = alloc_fw_cache_entry(name);
  1186. if (fce) {
  1187. list_add(&fce->list, &fwc->fw_names);
  1188. kref_get(&fw_priv->ref);
  1189. pr_debug("%s: fw: %s\n", __func__, name);
  1190. }
  1191. found:
  1192. spin_unlock(&fwc->name_lock);
  1193. }
  1194. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  1195. {
  1196. kfree_const(fce->name);
  1197. kfree(fce);
  1198. }
  1199. static void __async_dev_cache_fw_image(void *fw_entry,
  1200. async_cookie_t cookie)
  1201. {
  1202. struct fw_cache_entry *fce = fw_entry;
  1203. struct firmware_cache *fwc = &fw_cache;
  1204. int ret;
  1205. ret = cache_firmware(fce->name);
  1206. if (ret) {
  1207. spin_lock(&fwc->name_lock);
  1208. list_del(&fce->list);
  1209. spin_unlock(&fwc->name_lock);
  1210. free_fw_cache_entry(fce);
  1211. }
  1212. }
  1213. /* called with dev->devres_lock held */
  1214. static void dev_create_fw_entry(struct device *dev, void *res,
  1215. void *data)
  1216. {
  1217. struct fw_name_devm *fwn = res;
  1218. const char *fw_name = fwn->name;
  1219. struct list_head *head = data;
  1220. struct fw_cache_entry *fce;
  1221. fce = alloc_fw_cache_entry(fw_name);
  1222. if (fce)
  1223. list_add(&fce->list, head);
  1224. }
  1225. static int devm_name_match(struct device *dev, void *res,
  1226. void *match_data)
  1227. {
  1228. struct fw_name_devm *fwn = res;
  1229. return (fwn->magic == (unsigned long)match_data);
  1230. }
  1231. static void dev_cache_fw_image(struct device *dev, void *data)
  1232. {
  1233. LIST_HEAD(todo);
  1234. struct fw_cache_entry *fce;
  1235. struct fw_cache_entry *fce_next;
  1236. struct firmware_cache *fwc = &fw_cache;
  1237. devres_for_each_res(dev, fw_name_devm_release,
  1238. devm_name_match, &fw_cache,
  1239. dev_create_fw_entry, &todo);
  1240. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  1241. list_del(&fce->list);
  1242. spin_lock(&fwc->name_lock);
  1243. /* only one cache entry for one firmware */
  1244. if (!__fw_entry_found(fce->name)) {
  1245. list_add(&fce->list, &fwc->fw_names);
  1246. } else {
  1247. free_fw_cache_entry(fce);
  1248. fce = NULL;
  1249. }
  1250. spin_unlock(&fwc->name_lock);
  1251. if (fce)
  1252. async_schedule_domain(__async_dev_cache_fw_image,
  1253. (void *)fce,
  1254. &fw_cache_domain);
  1255. }
  1256. }
  1257. static void __device_uncache_fw_images(void)
  1258. {
  1259. struct firmware_cache *fwc = &fw_cache;
  1260. struct fw_cache_entry *fce;
  1261. spin_lock(&fwc->name_lock);
  1262. while (!list_empty(&fwc->fw_names)) {
  1263. fce = list_entry(fwc->fw_names.next,
  1264. struct fw_cache_entry, list);
  1265. list_del(&fce->list);
  1266. spin_unlock(&fwc->name_lock);
  1267. uncache_firmware(fce->name);
  1268. free_fw_cache_entry(fce);
  1269. spin_lock(&fwc->name_lock);
  1270. }
  1271. spin_unlock(&fwc->name_lock);
  1272. }
  1273. /**
  1274. * device_cache_fw_images() - cache devices' firmware
  1275. *
  1276. * If one device called request_firmware or its nowait version
  1277. * successfully before, the firmware names are recored into the
  1278. * device's devres link list, so device_cache_fw_images can call
  1279. * cache_firmware() to cache these firmwares for the device,
  1280. * then the device driver can load its firmwares easily at
  1281. * time when system is not ready to complete loading firmware.
  1282. */
  1283. static void device_cache_fw_images(void)
  1284. {
  1285. struct firmware_cache *fwc = &fw_cache;
  1286. DEFINE_WAIT(wait);
  1287. pr_debug("%s\n", __func__);
  1288. /* cancel uncache work */
  1289. cancel_delayed_work_sync(&fwc->work);
  1290. fw_fallback_set_cache_timeout();
  1291. mutex_lock(&fw_lock);
  1292. fwc->state = FW_LOADER_START_CACHE;
  1293. dpm_for_each_dev(NULL, dev_cache_fw_image);
  1294. mutex_unlock(&fw_lock);
  1295. /* wait for completion of caching firmware for all devices */
  1296. async_synchronize_full_domain(&fw_cache_domain);
  1297. fw_fallback_set_default_timeout();
  1298. }
  1299. /**
  1300. * device_uncache_fw_images() - uncache devices' firmware
  1301. *
  1302. * uncache all firmwares which have been cached successfully
  1303. * by device_uncache_fw_images earlier
  1304. */
  1305. static void device_uncache_fw_images(void)
  1306. {
  1307. pr_debug("%s\n", __func__);
  1308. __device_uncache_fw_images();
  1309. }
  1310. static void device_uncache_fw_images_work(struct work_struct *work)
  1311. {
  1312. device_uncache_fw_images();
  1313. }
  1314. /**
  1315. * device_uncache_fw_images_delay() - uncache devices firmwares
  1316. * @delay: number of milliseconds to delay uncache device firmwares
  1317. *
  1318. * uncache all devices's firmwares which has been cached successfully
  1319. * by device_cache_fw_images after @delay milliseconds.
  1320. */
  1321. static void device_uncache_fw_images_delay(unsigned long delay)
  1322. {
  1323. queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
  1324. msecs_to_jiffies(delay));
  1325. }
  1326. static int fw_pm_notify(struct notifier_block *notify_block,
  1327. unsigned long mode, void *unused)
  1328. {
  1329. switch (mode) {
  1330. case PM_HIBERNATION_PREPARE:
  1331. case PM_SUSPEND_PREPARE:
  1332. case PM_RESTORE_PREPARE:
  1333. /*
  1334. * Here, kill pending fallback requests will only kill
  1335. * non-uevent firmware request to avoid stalling suspend.
  1336. */
  1337. kill_pending_fw_fallback_reqs(false);
  1338. device_cache_fw_images();
  1339. break;
  1340. case PM_POST_SUSPEND:
  1341. case PM_POST_HIBERNATION:
  1342. case PM_POST_RESTORE:
  1343. /*
  1344. * In case that system sleep failed and syscore_suspend is
  1345. * not called.
  1346. */
  1347. mutex_lock(&fw_lock);
  1348. fw_cache.state = FW_LOADER_NO_CACHE;
  1349. mutex_unlock(&fw_lock);
  1350. device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
  1351. break;
  1352. }
  1353. return 0;
  1354. }
  1355. /* stop caching firmware once syscore_suspend is reached */
  1356. static int fw_suspend(void *data)
  1357. {
  1358. fw_cache.state = FW_LOADER_NO_CACHE;
  1359. return 0;
  1360. }
  1361. static const struct syscore_ops fw_syscore_ops = {
  1362. .suspend = fw_suspend,
  1363. };
  1364. static struct syscore fw_syscore = {
  1365. .ops = &fw_syscore_ops,
  1366. };
  1367. static int __init register_fw_pm_ops(void)
  1368. {
  1369. int ret;
  1370. spin_lock_init(&fw_cache.name_lock);
  1371. INIT_LIST_HEAD(&fw_cache.fw_names);
  1372. INIT_DELAYED_WORK(&fw_cache.work,
  1373. device_uncache_fw_images_work);
  1374. fw_cache.pm_notify.notifier_call = fw_pm_notify;
  1375. ret = register_pm_notifier(&fw_cache.pm_notify);
  1376. if (ret)
  1377. return ret;
  1378. register_syscore(&fw_syscore);
  1379. return ret;
  1380. }
  1381. static inline void unregister_fw_pm_ops(void)
  1382. {
  1383. unregister_syscore(&fw_syscore);
  1384. unregister_pm_notifier(&fw_cache.pm_notify);
  1385. }
  1386. #else
  1387. static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
  1388. {
  1389. }
  1390. static inline int register_fw_pm_ops(void)
  1391. {
  1392. return 0;
  1393. }
  1394. static inline void unregister_fw_pm_ops(void)
  1395. {
  1396. }
  1397. #endif
  1398. static void __init fw_cache_init(void)
  1399. {
  1400. spin_lock_init(&fw_cache.lock);
  1401. INIT_LIST_HEAD(&fw_cache.head);
  1402. fw_cache.state = FW_LOADER_NO_CACHE;
  1403. }
  1404. static int fw_shutdown_notify(struct notifier_block *unused1,
  1405. unsigned long unused2, void *unused3)
  1406. {
  1407. /*
  1408. * Kill all pending fallback requests to avoid both stalling shutdown,
  1409. * and avoid a deadlock with the usermode_lock.
  1410. */
  1411. kill_pending_fw_fallback_reqs(true);
  1412. return NOTIFY_DONE;
  1413. }
  1414. static struct notifier_block fw_shutdown_nb = {
  1415. .notifier_call = fw_shutdown_notify,
  1416. };
  1417. static int __init firmware_class_init(void)
  1418. {
  1419. int ret;
  1420. /* No need to unfold these on exit */
  1421. fw_cache_init();
  1422. ret = register_fw_pm_ops();
  1423. if (ret)
  1424. return ret;
  1425. ret = register_reboot_notifier(&fw_shutdown_nb);
  1426. if (ret)
  1427. goto out;
  1428. return register_sysfs_loader();
  1429. out:
  1430. unregister_fw_pm_ops();
  1431. return ret;
  1432. }
  1433. static void __exit firmware_class_exit(void)
  1434. {
  1435. unregister_fw_pm_ops();
  1436. unregister_reboot_notifier(&fw_shutdown_nb);
  1437. unregister_sysfs_loader();
  1438. }
  1439. fs_initcall(firmware_class_init);
  1440. module_exit(firmware_class_exit);