xref: /aosp_15_r20/system/core/init/README.ueventd.md (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1*00c7fec1SAndroid Build Coastguard Worker# Ueventd
2*00c7fec1SAndroid Build Coastguard Worker-------
3*00c7fec1SAndroid Build Coastguard WorkerUeventd manages `/dev`, sets permissions for `/sys`, and handles firmware uevents. It has default
4*00c7fec1SAndroid Build Coastguard Workerbehavior described below, along with a scripting language that allows customizing this behavior,
5*00c7fec1SAndroid Build Coastguard Workerbuilt on the same parser as init.
6*00c7fec1SAndroid Build Coastguard Worker
7*00c7fec1SAndroid Build Coastguard WorkerUeventd has one generic customization parameter, the size of rcvbuf_size for the ueventd socket. It
8*00c7fec1SAndroid Build Coastguard Workeris customized by the `uevent_socket_rcvbuf_size` parameter, which takes the format of
9*00c7fec1SAndroid Build Coastguard Worker
10*00c7fec1SAndroid Build Coastguard Worker    uevent_socket_rcvbuf_size <size>
11*00c7fec1SAndroid Build Coastguard WorkerFor example
12*00c7fec1SAndroid Build Coastguard Worker
13*00c7fec1SAndroid Build Coastguard Worker    uevent_socket_rcvbuf_size 16M
14*00c7fec1SAndroid Build Coastguard WorkerSets the uevent socket rcvbuf_size to 16 megabytes.
15*00c7fec1SAndroid Build Coastguard Worker
16*00c7fec1SAndroid Build Coastguard Worker## Importing configuration files
17*00c7fec1SAndroid Build Coastguard Worker--------------------------------
18*00c7fec1SAndroid Build Coastguard WorkerUeventd reads /system/etc/ueventd.rc, all other files are imported via the `import` command, which
19*00c7fec1SAndroid Build Coastguard Workertakes the format of
20*00c7fec1SAndroid Build Coastguard Worker
21*00c7fec1SAndroid Build Coastguard Worker    import <path>
22*00c7fec1SAndroid Build Coastguard WorkerThis command parses an ueventd config file, extending the current configuration.  If _path_ is a
23*00c7fec1SAndroid Build Coastguard Workerdirectory, each file in the directory is parsed as a config file. It is not recursive, nested
24*00c7fec1SAndroid Build Coastguard Workerdirectories will not be parsed.  Imported files are parsed after the current file has been parsed.
25*00c7fec1SAndroid Build Coastguard Worker
26*00c7fec1SAndroid Build Coastguard Worker## /dev
27*00c7fec1SAndroid Build Coastguard Worker----
28*00c7fec1SAndroid Build Coastguard WorkerUeventd listens to the kernel uevent sockets and creates/deletes nodes in `/dev` based on the
29*00c7fec1SAndroid Build Coastguard Workerincoming add/remove uevents. It defaults to using `0600` mode and `root` user/group. It always
30*00c7fec1SAndroid Build Coastguard Workercreates the nodes with the SELabel from the current loaded SEPolicy. It has three default behaviors
31*00c7fec1SAndroid Build Coastguard Workerfor the node path:
32*00c7fec1SAndroid Build Coastguard Worker
33*00c7fec1SAndroid Build Coastguard Worker  1. Block devices are created as `/dev/block/<basename uevent DEVPATH>`. There are symlinks created
34*00c7fec1SAndroid Build Coastguard Worker     to this node at `/dev/block/<type>/<parent device>/<basename uevent DEVPATH>`,
35*00c7fec1SAndroid Build Coastguard Worker     `/dev/block/<type>/<parent device>/by-name/<uevent PARTNAME>`, and `/dev/block/by-name/<uevent
36*00c7fec1SAndroid Build Coastguard Worker     PARTNAME>` if the device is a boot device.
37*00c7fec1SAndroid Build Coastguard Worker  2. USB devices are created as `/dev/<uevent DEVNAME>` if `DEVNAME` was specified for the uevent,
38*00c7fec1SAndroid Build Coastguard Worker     otherwise as `/dev/bus/usb/<bus_id>/<device_id>` where `bus_id` is `uevent MINOR / 128 + 1` and
39*00c7fec1SAndroid Build Coastguard Worker     `device_id` is `uevent MINOR % 128 + 1`.
40*00c7fec1SAndroid Build Coastguard Worker  3. All other devices are created as `/dev/<basename uevent DEVPATH>`
41*00c7fec1SAndroid Build Coastguard Worker
42*00c7fec1SAndroid Build Coastguard WorkerWhether a device is considered a "boot device" is a bit complicated.
43*00c7fec1SAndroid Build Coastguard Worker
44*00c7fec1SAndroid Build Coastguard Worker - The recommended way to specify the boot device is to provide the "partition UUID" containing the
45*00c7fec1SAndroid Build Coastguard Worker   kernel (or, really, any parition on the boot device) and then boot device is the block device
46*00c7fec1SAndroid Build Coastguard Worker   containing that partition. This is passed via `androidboot.boot_part_uuid` which can be provided
47*00c7fec1SAndroid Build Coastguard Worker   either via the kernel bootconfig or via the kernel commandline. As an example, you could set
48*00c7fec1SAndroid Build Coastguard Worker   `androidboot.boot_part_uuid=12345678-abcd-ef01-0234-6789abcdef01`.
49*00c7fec1SAndroid Build Coastguard Worker - Though using `boot_part_uuid` is preferred, you can also specify the boot device via
50*00c7fec1SAndroid Build Coastguard Worker   `androidboot.boot_device` or `androidboot.boot_devices`. These can be passed via the kernel
51*00c7fec1SAndroid Build Coastguard Worker   bootconfig or the kernel command line. It is also possible to pass this via device tree by
52*00c7fec1SAndroid Build Coastguard Worker   creating a `boot_devices` property in the Android firmware node. In most cases the `boot_device`
53*00c7fec1SAndroid Build Coastguard Worker   is the sysfs path (without the `/sys/devices` or `/sys/devices/platform` prefix) to the closest
54*00c7fec1SAndroid Build Coastguard Worker   parent of the block device that's on the "platform" bus. As an example, if the block device is
55*00c7fec1SAndroid Build Coastguard Worker   `/sys/devices/platform/soc@0/7c4000.mmc/mmc_host/mmc1/mmc1:0001/block/mmcblk1` then the
56*00c7fec1SAndroid Build Coastguard Worker   `boot_device` is `soc@0/7c4000.mmc` since we strip off the `/sys/devices/platform` and nothing
57*00c7fec1SAndroid Build Coastguard Worker   past the `7c4000.mmc` directory represents a device on the "platform" bus. In the case that none
58*00c7fec1SAndroid Build Coastguard Worker   of the parents are on the "platform" bus there are special rules for block devices under PCI
59*00c7fec1SAndroid Build Coastguard Worker   and VBD (Virtual Block Device). NOTE: sysfs paths for block devices are not guaranteed to be
60*00c7fec1SAndroid Build Coastguard Worker   stable between kernel versions, which is one of the reasons why it is suggested to use
61*00c7fec1SAndroid Build Coastguard Worker   `boot_part_uuid` instead of `boot_devices`. ALSO NOTE: If more than one device matches (either
62*00c7fec1SAndroid Build Coastguard Worker   because multiple `boot_devices` were listed or because there was more than one block device
63*00c7fec1SAndroid Build Coastguard Worker   under the found sysfs directory) and these multiple matching devices provide some of the same
64*00c7fec1SAndroid Build Coastguard Worker   named partitions then the behavior is unspecified.
65*00c7fec1SAndroid Build Coastguard Worker - There is a further fallback to determine "boot devices" via the vstab, but providing at least
66*00c7fec1SAndroid Build Coastguard Worker   `boot_devices` has been required since Android 12 so this further fallback will not be described
67*00c7fec1SAndroid Build Coastguard Worker   here.
68*00c7fec1SAndroid Build Coastguard Worker
69*00c7fec1SAndroid Build Coastguard WorkerThe permissions can be modified using a ueventd.rc script and a line that beings with `/dev`. These
70*00c7fec1SAndroid Build Coastguard Workerlines take the format of
71*00c7fec1SAndroid Build Coastguard Worker
72*00c7fec1SAndroid Build Coastguard Worker    devname mode uid gid [options]
73*00c7fec1SAndroid Build Coastguard WorkerFor example
74*00c7fec1SAndroid Build Coastguard Worker
75*00c7fec1SAndroid Build Coastguard Worker    /dev/null 0666 root root
76*00c7fec1SAndroid Build Coastguard WorkerWhen `/dev/null` is created, its mode will be set to `0666`, its user to `root` and its group to
77*00c7fec1SAndroid Build Coastguard Worker`root`.
78*00c7fec1SAndroid Build Coastguard Worker
79*00c7fec1SAndroid Build Coastguard WorkerThe path can be modified using a ueventd.rc script and a `subsystem` and/or `driver` section.
80*00c7fec1SAndroid Build Coastguard WorkerThere are three options to set for a subsystem or driver: the name, which device name to use,
81*00c7fec1SAndroid Build Coastguard Workerand which directory to place the device in. The section takes the below format of
82*00c7fec1SAndroid Build Coastguard Worker
83*00c7fec1SAndroid Build Coastguard Worker    subsystem <subsystem_name>
84*00c7fec1SAndroid Build Coastguard Worker      devname uevent_devname|uevent_devpath
85*00c7fec1SAndroid Build Coastguard Worker      [dirname <directory>]
86*00c7fec1SAndroid Build Coastguard Worker
87*00c7fec1SAndroid Build Coastguard Worker`subsystem_name` is used to match the uevent `SUBSYSTEM` value.
88*00c7fec1SAndroid Build Coastguard Worker
89*00c7fec1SAndroid Build Coastguard Worker`devname` takes one of three options:
90*00c7fec1SAndroid Build Coastguard Worker  1. `uevent_devname` specifies that the name of the node will be the uevent `DEVNAME`
91*00c7fec1SAndroid Build Coastguard Worker  2. `uevent_devpath` specifies that the name of the node will be basename uevent `DEVPATH`
92*00c7fec1SAndroid Build Coastguard Worker  3. `sys_name` specifies that the name of the node will be the contents of `/sys/DEVPATH/name`
93*00c7fec1SAndroid Build Coastguard Worker
94*00c7fec1SAndroid Build Coastguard Worker`dirname` is an optional parameter that specifies a directory within `/dev` where the node will be
95*00c7fec1SAndroid Build Coastguard Workercreated.
96*00c7fec1SAndroid Build Coastguard Worker
97*00c7fec1SAndroid Build Coastguard WorkerFor example
98*00c7fec1SAndroid Build Coastguard Worker
99*00c7fec1SAndroid Build Coastguard Worker    subsystem sound
100*00c7fec1SAndroid Build Coastguard Worker      devname uevent_devpath
101*00c7fec1SAndroid Build Coastguard Worker      dirname /dev/snd
102*00c7fec1SAndroid Build Coastguard Workerindicates that all uevents with `SUBSYSTEM=sound` will create nodes as `/dev/snd/<basename uevent
103*00c7fec1SAndroid Build Coastguard WorkerDEVPATH>`.
104*00c7fec1SAndroid Build Coastguard Worker
105*00c7fec1SAndroid Build Coastguard WorkerThe `driver` section has the exact same structure as a `subsystem` section, but
106*00c7fec1SAndroid Build Coastguard Workerwill instead match the `DRIVER` value in a `bind`/`unbind` uevent. However, the
107*00c7fec1SAndroid Build Coastguard Worker`driver` section will be ignored for block devices.
108*00c7fec1SAndroid Build Coastguard Worker
109*00c7fec1SAndroid Build Coastguard Worker## /sys
110*00c7fec1SAndroid Build Coastguard Worker----
111*00c7fec1SAndroid Build Coastguard WorkerUeventd by default takes no action for `/sys`, however it can be instructed to set permissions for
112*00c7fec1SAndroid Build Coastguard Workercertain files in `/sys` when matching uevents are generated. This is done using a ueventd.rc script
113*00c7fec1SAndroid Build Coastguard Workerand a line that begins with `/sys`. These lines take the format of
114*00c7fec1SAndroid Build Coastguard Worker
115*00c7fec1SAndroid Build Coastguard Worker    nodename attr mode uid gid [options]
116*00c7fec1SAndroid Build Coastguard WorkerFor example
117*00c7fec1SAndroid Build Coastguard Worker
118*00c7fec1SAndroid Build Coastguard Worker    /sys/devices/system/cpu/cpu* cpufreq/scaling_max_freq 0664 system system
119*00c7fec1SAndroid Build Coastguard WorkerWhen a uevent that matches the pattern `/sys/devices/system/cpu/cpu*` is sent, the matching sysfs
120*00c7fec1SAndroid Build Coastguard Workerattribute, `cpufreq/scaling_max_freq`, will have its mode set to `0664`, its user to to `system` and
121*00c7fec1SAndroid Build Coastguard Workerits group set to `system`.
122*00c7fec1SAndroid Build Coastguard Worker
123*00c7fec1SAndroid Build Coastguard Worker## Path matching
124*00c7fec1SAndroid Build Coastguard Worker----------------
125*00c7fec1SAndroid Build Coastguard WorkerThe path for a `/dev` or `/sys` entry can contain a `*` anywhere in the path.
126*00c7fec1SAndroid Build Coastguard Worker1. If the only `*` appears at the end of the string or if the _options_ parameter is set to
127*00c7fec1SAndroid Build Coastguard Worker`no_fnm_pathname`, ueventd matches the entry by `fnmatch(entry_path, incoming_path, 0)`
128*00c7fec1SAndroid Build Coastguard Worker2. Otherwise, ueventd matches the entry by `fnmatch(entry_path, incoming_path, FNM_PATHNAME)`
129*00c7fec1SAndroid Build Coastguard Worker
130*00c7fec1SAndroid Build Coastguard WorkerSee the [man page for fnmatch](https://www.man7.org/linux/man-pages/man3/fnmatch.3.html) for more
131*00c7fec1SAndroid Build Coastguard Workerdetails.
132*00c7fec1SAndroid Build Coastguard Worker
133*00c7fec1SAndroid Build Coastguard Worker## Firmware loading
134*00c7fec1SAndroid Build Coastguard Worker----------------
135*00c7fec1SAndroid Build Coastguard WorkerUeventd by default serves firmware requests by searching through a list of firmware directories
136*00c7fec1SAndroid Build Coastguard Workerfor a file matching the uevent `FIRMWARE`. It then forks a process to serve this firmware to the
137*00c7fec1SAndroid Build Coastguard Workerkernel.
138*00c7fec1SAndroid Build Coastguard Worker
139*00c7fec1SAndroid Build Coastguard Worker`/apex/*/etc/firmware` is also searched after a list of firmware directories.
140*00c7fec1SAndroid Build Coastguard Worker
141*00c7fec1SAndroid Build Coastguard WorkerThe list of firmware directories is customized by a `firmware_directories` line in a ueventd.rc
142*00c7fec1SAndroid Build Coastguard Workerfile. This line takes the format of
143*00c7fec1SAndroid Build Coastguard Worker
144*00c7fec1SAndroid Build Coastguard Worker    firmware_directories <firmware_directory> [ <firmware_directory> ]*
145*00c7fec1SAndroid Build Coastguard WorkerFor example
146*00c7fec1SAndroid Build Coastguard Worker
147*00c7fec1SAndroid Build Coastguard Worker    firmware_directories /etc/firmware/ /odm/firmware/ /vendor/firmware/ /firmware/image/
148*00c7fec1SAndroid Build Coastguard WorkerAdds those 4 directories, in that order to the list of firmware directories that will be tried by
149*00c7fec1SAndroid Build Coastguard Workerueventd. Note that this option always accumulates to the list; it is not possible to remove previous
150*00c7fec1SAndroid Build Coastguard Workerentries.
151*00c7fec1SAndroid Build Coastguard Worker
152*00c7fec1SAndroid Build Coastguard WorkerUeventd will wait until after `post-fs` in init, to keep retrying before believing the firmwares are
153*00c7fec1SAndroid Build Coastguard Workernot present.
154*00c7fec1SAndroid Build Coastguard Worker
155*00c7fec1SAndroid Build Coastguard WorkerThe exact firmware file to be served can be customized by running an external program by a
156*00c7fec1SAndroid Build Coastguard Worker`external_firmware_handler` line in a ueventd.rc file. This line takes the format of
157*00c7fec1SAndroid Build Coastguard Worker
158*00c7fec1SAndroid Build Coastguard Worker    external_firmware_handler <devpath> <user [group]> <path to external program>
159*00c7fec1SAndroid Build Coastguard Worker
160*00c7fec1SAndroid Build Coastguard WorkerThe handler will be run as the given user, or if a group is provided, as the given user and group.
161*00c7fec1SAndroid Build Coastguard Worker
162*00c7fec1SAndroid Build Coastguard WorkerFor example
163*00c7fec1SAndroid Build Coastguard Worker
164*00c7fec1SAndroid Build Coastguard Worker    external_firmware_handler /devices/leds/red/firmware/coeffs.bin system /vendor/bin/led_coeffs.bin
165*00c7fec1SAndroid Build Coastguard WorkerWill launch `/vendor/bin/led_coeffs.bin` as the system user instead of serving the default firmware
166*00c7fec1SAndroid Build Coastguard Workerfor `/devices/leds/red/firmware/coeffs.bin`.
167*00c7fec1SAndroid Build Coastguard Worker
168*00c7fec1SAndroid Build Coastguard WorkerThe `devpath` argument may include asterisks (`*`) to match multiple paths. For example, the string
169*00c7fec1SAndroid Build Coastguard Worker`/dev/*/red` will match `/dev/leds/red` as well as `/dev/lights/red`. The pattern matching follows
170*00c7fec1SAndroid Build Coastguard Workerthe rules of the fnmatch() function.
171*00c7fec1SAndroid Build Coastguard Worker
172*00c7fec1SAndroid Build Coastguard WorkerUeventd will provide the uevent `DEVPATH` and `FIRMWARE` to this external program on the environment
173*00c7fec1SAndroid Build Coastguard Workervia environment variables with the same names. Ueventd will use the string written to stdout as the
174*00c7fec1SAndroid Build Coastguard Workernew name of the firmware to load. It will still look for the new firmware in the list of firmware
175*00c7fec1SAndroid Build Coastguard Workerdirectories stated above. It will also reject file names with `..` in them, to prevent leaving these
176*00c7fec1SAndroid Build Coastguard Workerdirectories. If stdout cannot be read, or the program returns with any exit code other than
177*00c7fec1SAndroid Build Coastguard Worker`EXIT_SUCCESS`, or the program crashes, the default firmware from the uevent will be loaded.
178*00c7fec1SAndroid Build Coastguard Worker
179*00c7fec1SAndroid Build Coastguard WorkerUeventd will additionally log all messages sent to stderr from the external program to the serial
180*00c7fec1SAndroid Build Coastguard Workerconsole after the external program has exited.
181*00c7fec1SAndroid Build Coastguard Worker
182*00c7fec1SAndroid Build Coastguard WorkerIf the kernel command-line argument `firmware_class.path` is set, this path
183*00c7fec1SAndroid Build Coastguard Workerwill be used first by the kernel to search for the firmware files. If found,
184*00c7fec1SAndroid Build Coastguard Workerueventd will not be called at all. See the
185*00c7fec1SAndroid Build Coastguard Worker[kernel documentation](https://www.kernel.org/doc/html/v5.10/driver-api/firmware/fw_search_path.html)
186*00c7fec1SAndroid Build Coastguard Workerfor more details on this feature.
187*00c7fec1SAndroid Build Coastguard Worker
188*00c7fec1SAndroid Build Coastguard Worker## Coldboot
189*00c7fec1SAndroid Build Coastguard Worker--------
190*00c7fec1SAndroid Build Coastguard WorkerUeventd must create devices in `/dev` for all devices that have already sent their uevents before
191*00c7fec1SAndroid Build Coastguard Workerueventd has started. To do so, when ueventd is started it does what it calls a 'coldboot' on `/sys`,
192*00c7fec1SAndroid Build Coastguard Workerin which it writes 'add' to every 'uevent' file that it finds in `/sys/class`, `/sys/block`, and
193*00c7fec1SAndroid Build Coastguard Worker`/sys/devices`. This causes the kernel to regenerate the uevents for these paths, and thus for
194*00c7fec1SAndroid Build Coastguard Workerueventd to create the nodes.
195*00c7fec1SAndroid Build Coastguard Worker
196*00c7fec1SAndroid Build Coastguard WorkerFor boot time purposes, this is done in parallel across a set of child processes. `ueventd.cpp` in
197*00c7fec1SAndroid Build Coastguard Workerthis directory contains documentation on how the parallelization is done.
198*00c7fec1SAndroid Build Coastguard Worker
199*00c7fec1SAndroid Build Coastguard WorkerThere is an option to parallelize the restorecon function during cold boot as well. It is
200*00c7fec1SAndroid Build Coastguard Workerrecommended that devices use genfscon for labeling sysfs nodes. However, some devices may benefit
201*00c7fec1SAndroid Build Coastguard Workerfrom enabling the parallelization option:
202*00c7fec1SAndroid Build Coastguard Worker
203*00c7fec1SAndroid Build Coastguard Worker    parallel_restorecon enabled
204*00c7fec1SAndroid Build Coastguard Worker
205*00c7fec1SAndroid Build Coastguard WorkerDo parallel restorecon to speed up boot process, subdirectories under `/sys`
206*00c7fec1SAndroid Build Coastguard Workercan be sliced by ueventd.rc, and run on multiple process.
207*00c7fec1SAndroid Build Coastguard Worker    parallel_restorecon_dir <directory>
208*00c7fec1SAndroid Build Coastguard Worker
209*00c7fec1SAndroid Build Coastguard WorkerFor example
210*00c7fec1SAndroid Build Coastguard Worker    parallel_restorecon_dir /sys
211*00c7fec1SAndroid Build Coastguard Worker    parallel_restorecon_dir /sys/devices
212*00c7fec1SAndroid Build Coastguard Worker    parallel_restorecon_dir /sys/devices/platform
213*00c7fec1SAndroid Build Coastguard Worker    parallel_restorecon_dir /sys/devices/platform/soc
214