1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/highmem.h>
4 #include <linux/module.h>
5 #include <linux/security.h>
6 #include <linux/slab.h>
7 #include <linux/types.h>
12 * sysfs support for firmware loader
15 void __fw_load_abort(struct fw_priv
*fw_priv
)
18 * There is a small window in which user can write to 'loading'
19 * between loading done/aborted and disappearance of 'loading'
21 if (fw_state_is_aborted(fw_priv
) || fw_state_is_done(fw_priv
))
24 fw_state_aborted(fw_priv
);
27 #ifdef CONFIG_FW_LOADER_USER_HELPER
28 static ssize_t
timeout_show(const struct class *class, const struct class_attribute
*attr
,
31 return sysfs_emit(buf
, "%d\n", __firmware_loading_timeout());
35 * timeout_store() - set number of seconds to wait for firmware
36 * @class: device class pointer
37 * @attr: device attribute pointer
38 * @buf: buffer to scan for timeout value
39 * @count: number of bytes in @buf
41 * Sets the number of seconds to wait for the firmware. Once
42 * this expires an error will be returned to the driver and no
43 * firmware will be provided.
45 * Note: zero means 'wait forever'.
47 static ssize_t
timeout_store(const struct class *class, const struct class_attribute
*attr
,
48 const char *buf
, size_t count
)
50 int tmp_loading_timeout
= simple_strtol(buf
, NULL
, 10);
52 if (tmp_loading_timeout
< 0)
53 tmp_loading_timeout
= 0;
55 __fw_fallback_set_timeout(tmp_loading_timeout
);
59 static CLASS_ATTR_RW(timeout
);
61 static struct attribute
*firmware_class_attrs
[] = {
62 &class_attr_timeout
.attr
,
65 ATTRIBUTE_GROUPS(firmware_class
);
67 static int do_firmware_uevent(const struct fw_sysfs
*fw_sysfs
, struct kobj_uevent_env
*env
)
69 if (add_uevent_var(env
, "FIRMWARE=%s", fw_sysfs
->fw_priv
->fw_name
))
71 if (add_uevent_var(env
, "TIMEOUT=%i", __firmware_loading_timeout()))
73 if (add_uevent_var(env
, "ASYNC=%d", fw_sysfs
->nowait
))
79 static int firmware_uevent(const struct device
*dev
, struct kobj_uevent_env
*env
)
81 const struct fw_sysfs
*fw_sysfs
= to_fw_sysfs(dev
);
85 if (fw_sysfs
->fw_priv
)
86 err
= do_firmware_uevent(fw_sysfs
, env
);
87 mutex_unlock(&fw_lock
);
90 #endif /* CONFIG_FW_LOADER_USER_HELPER */
92 static void fw_dev_release(struct device
*dev
)
94 struct fw_sysfs
*fw_sysfs
= to_fw_sysfs(dev
);
96 if (fw_sysfs
->fw_upload_priv
)
97 fw_upload_free(fw_sysfs
);
102 static struct class firmware_class
= {
104 #ifdef CONFIG_FW_LOADER_USER_HELPER
105 .class_groups
= firmware_class_groups
,
106 .dev_uevent
= firmware_uevent
,
108 .dev_release
= fw_dev_release
,
111 int register_sysfs_loader(void)
113 int ret
= class_register(&firmware_class
);
117 return register_firmware_config_sysctl();
120 void unregister_sysfs_loader(void)
122 unregister_firmware_config_sysctl();
123 class_unregister(&firmware_class
);
126 static ssize_t
firmware_loading_show(struct device
*dev
,
127 struct device_attribute
*attr
, char *buf
)
129 struct fw_sysfs
*fw_sysfs
= to_fw_sysfs(dev
);
132 mutex_lock(&fw_lock
);
133 if (fw_sysfs
->fw_priv
)
134 loading
= fw_state_is_loading(fw_sysfs
->fw_priv
);
135 mutex_unlock(&fw_lock
);
137 return sysfs_emit(buf
, "%d\n", loading
);
141 * firmware_loading_store() - set value in the 'loading' control file
142 * @dev: device pointer
143 * @attr: device attribute pointer
144 * @buf: buffer to scan for loading control value
145 * @count: number of bytes in @buf
147 * The relevant values are:
149 * 1: Start a load, discarding any previous partial load.
150 * 0: Conclude the load and hand the data to the driver code.
151 * -1: Conclude the load with an error and discard any written data.
153 static ssize_t
firmware_loading_store(struct device
*dev
,
154 struct device_attribute
*attr
,
155 const char *buf
, size_t count
)
157 struct fw_sysfs
*fw_sysfs
= to_fw_sysfs(dev
);
158 struct fw_priv
*fw_priv
;
159 ssize_t written
= count
;
160 int loading
= simple_strtol(buf
, NULL
, 10);
162 mutex_lock(&fw_lock
);
163 fw_priv
= fw_sysfs
->fw_priv
;
164 if (fw_state_is_aborted(fw_priv
) || fw_state_is_done(fw_priv
))
169 /* discarding any previous partial load */
170 fw_free_paged_buf(fw_priv
);
171 fw_state_start(fw_priv
);
174 if (fw_state_is_loading(fw_priv
)) {
178 * Several loading requests may be pending on
179 * one same firmware buf, so let all requests
180 * see the mapped 'buf->data' once the loading
183 rc
= fw_map_paged_buf(fw_priv
);
185 dev_err(dev
, "%s: map pages failed\n",
188 rc
= security_kernel_post_load_data(fw_priv
->data
,
194 * Same logic as fw_load_abort, only the DONE bit
195 * is ignored and we set ABORT only on failure.
198 fw_state_aborted(fw_priv
);
201 fw_state_done(fw_priv
);
204 * If this is a user-initiated firmware upload
205 * then start the upload in a worker thread now.
207 rc
= fw_upload_start(fw_sysfs
);
215 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
218 fw_load_abort(fw_sysfs
);
219 if (fw_sysfs
->fw_upload_priv
)
220 fw_state_init(fw_sysfs
->fw_priv
);
225 mutex_unlock(&fw_lock
);
229 DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
231 static void firmware_rw_data(struct fw_priv
*fw_priv
, char *buffer
,
232 loff_t offset
, size_t count
, bool read
)
235 memcpy(buffer
, fw_priv
->data
+ offset
, count
);
237 memcpy(fw_priv
->data
+ offset
, buffer
, count
);
240 static void firmware_rw(struct fw_priv
*fw_priv
, char *buffer
,
241 loff_t offset
, size_t count
, bool read
)
244 int page_nr
= offset
>> PAGE_SHIFT
;
245 int page_ofs
= offset
& (PAGE_SIZE
- 1);
246 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
249 memcpy_from_page(buffer
, fw_priv
->pages
[page_nr
],
252 memcpy_to_page(fw_priv
->pages
[page_nr
], page_ofs
,
261 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
262 struct bin_attribute
*bin_attr
,
263 char *buffer
, loff_t offset
, size_t count
)
265 struct device
*dev
= kobj_to_dev(kobj
);
266 struct fw_sysfs
*fw_sysfs
= to_fw_sysfs(dev
);
267 struct fw_priv
*fw_priv
;
270 mutex_lock(&fw_lock
);
271 fw_priv
= fw_sysfs
->fw_priv
;
272 if (!fw_priv
|| fw_state_is_done(fw_priv
)) {
276 if (offset
> fw_priv
->size
) {
280 if (count
> fw_priv
->size
- offset
)
281 count
= fw_priv
->size
- offset
;
286 firmware_rw_data(fw_priv
, buffer
, offset
, count
, true);
288 firmware_rw(fw_priv
, buffer
, offset
, count
, true);
291 mutex_unlock(&fw_lock
);
295 static int fw_realloc_pages(struct fw_sysfs
*fw_sysfs
, int min_size
)
299 err
= fw_grow_paged_buf(fw_sysfs
->fw_priv
,
300 PAGE_ALIGN(min_size
) >> PAGE_SHIFT
);
302 fw_load_abort(fw_sysfs
);
307 * firmware_data_write() - write method for firmware
308 * @filp: open sysfs file
309 * @kobj: kobject for the device
310 * @bin_attr: bin_attr structure
311 * @buffer: buffer being written
312 * @offset: buffer offset for write in total data store area
313 * @count: buffer size
315 * Data written to the 'data' attribute will be later handed to
316 * the driver as a firmware image.
318 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
319 struct bin_attribute
*bin_attr
,
320 char *buffer
, loff_t offset
, size_t count
)
322 struct device
*dev
= kobj_to_dev(kobj
);
323 struct fw_sysfs
*fw_sysfs
= to_fw_sysfs(dev
);
324 struct fw_priv
*fw_priv
;
327 if (!capable(CAP_SYS_RAWIO
))
330 mutex_lock(&fw_lock
);
331 fw_priv
= fw_sysfs
->fw_priv
;
332 if (!fw_priv
|| fw_state_is_done(fw_priv
)) {
338 if (offset
+ count
> fw_priv
->allocated_size
) {
342 firmware_rw_data(fw_priv
, buffer
, offset
, count
, false);
345 retval
= fw_realloc_pages(fw_sysfs
, offset
+ count
);
350 firmware_rw(fw_priv
, buffer
, offset
, count
, false);
353 fw_priv
->size
= max_t(size_t, offset
+ count
, fw_priv
->size
);
355 mutex_unlock(&fw_lock
);
359 static struct bin_attribute firmware_attr_data
= {
360 .attr
= { .name
= "data", .mode
= 0644 },
362 .read
= firmware_data_read
,
363 .write
= firmware_data_write
,
366 static struct attribute
*fw_dev_attrs
[] = {
367 &dev_attr_loading
.attr
,
368 #ifdef CONFIG_FW_UPLOAD
369 &dev_attr_cancel
.attr
,
370 &dev_attr_status
.attr
,
371 &dev_attr_error
.attr
,
372 &dev_attr_remaining_size
.attr
,
377 static struct bin_attribute
*fw_dev_bin_attrs
[] = {
382 static const struct attribute_group fw_dev_attr_group
= {
383 .attrs
= fw_dev_attrs
,
384 .bin_attrs
= fw_dev_bin_attrs
,
385 #ifdef CONFIG_FW_UPLOAD
386 .is_visible
= fw_upload_is_visible
,
390 static const struct attribute_group
*fw_dev_attr_groups
[] = {
396 fw_create_instance(struct firmware
*firmware
, const char *fw_name
,
397 struct device
*device
, u32 opt_flags
)
399 struct fw_sysfs
*fw_sysfs
;
400 struct device
*f_dev
;
402 fw_sysfs
= kzalloc(sizeof(*fw_sysfs
), GFP_KERNEL
);
404 fw_sysfs
= ERR_PTR(-ENOMEM
);
408 fw_sysfs
->nowait
= !!(opt_flags
& FW_OPT_NOWAIT
);
409 fw_sysfs
->fw
= firmware
;
410 f_dev
= &fw_sysfs
->dev
;
412 device_initialize(f_dev
);
413 dev_set_name(f_dev
, "%s", fw_name
);
414 f_dev
->parent
= device
;
415 f_dev
->class = &firmware_class
;
416 f_dev
->groups
= fw_dev_attr_groups
;