xref: /qemu/docs/specs/fw_cfg.rst (revision 57c3d238a5ff7e7ad7aba098b5d55d8d89c2a6a1)
16cec43e1SGabriel L. SomloQEMU Firmware Configuration (fw_cfg) Device
26cec43e1SGabriel L. Somlo===========================================
36cec43e1SGabriel L. Somlo
46cec43e1SGabriel L. Somlo= Guest-side Hardware Interface =
56cec43e1SGabriel L. Somlo
66cec43e1SGabriel L. SomloThis hardware interface allows the guest to retrieve various data items
76cec43e1SGabriel L. Somlo(blobs) that can influence how the firmware configures itself, or may
86cec43e1SGabriel L. Somlocontain tables to be installed for the guest OS. Examples include device
96cec43e1SGabriel L. Somloboot order, ACPI and SMBIOS tables, virtual machine UUID, SMP and NUMA
106cec43e1SGabriel L. Somloinformation, kernel/initrd images for direct (Linux) kernel booting, etc.
116cec43e1SGabriel L. Somlo
126cec43e1SGabriel L. Somlo== Selector (Control) Register ==
136cec43e1SGabriel L. Somlo
146cec43e1SGabriel L. Somlo* Write only
156cec43e1SGabriel L. Somlo* Location: platform dependent (IOport or MMIO)
166cec43e1SGabriel L. Somlo* Width: 16-bit
176cec43e1SGabriel L. Somlo* Endianness: little-endian (if IOport), or big-endian (if MMIO)
186cec43e1SGabriel L. Somlo
196cec43e1SGabriel L. SomloA write to this register sets the index of a firmware configuration
206cec43e1SGabriel L. Somloitem which can subsequently be accessed via the data register.
216cec43e1SGabriel L. Somlo
226cec43e1SGabriel L. SomloSetting the selector register will cause the data offset to be set
236cec43e1SGabriel L. Somloto zero. The data offset impacts which data is accessed via the data
246cec43e1SGabriel L. Somloregister, and is explained below.
256cec43e1SGabriel L. Somlo
266cec43e1SGabriel L. SomloBit14 of the selector register indicates whether the configuration
276cec43e1SGabriel L. Somlosetting is being written. A value of 0 means the item is only being
286cec43e1SGabriel L. Somloread, and all write access to the data port will be ignored. A value
296cec43e1SGabriel L. Somloof 1 means the item's data can be overwritten by writes to the data
306cec43e1SGabriel L. Somloregister. In other words, configuration write mode is enabled when
316cec43e1SGabriel L. Somlothe selector value is between 0x4000-0x7fff or 0xc000-0xffff.
326cec43e1SGabriel L. Somlo
336cec43e1SGabriel L. SomloNOTE: As of QEMU v2.4, writes to the fw_cfg data register are no
346cec43e1SGabriel L. Somlo      longer supported, and will be ignored (treated as no-ops)!
356cec43e1SGabriel L. Somlo
366cec43e1SGabriel L. SomloBit15 of the selector register indicates whether the configuration
376cec43e1SGabriel L. Somlosetting is architecture specific. A value of 0 means the item is a
386cec43e1SGabriel L. Somlogeneric configuration item. A value of 1 means the item is specific
396cec43e1SGabriel L. Somloto a particular architecture. In other words, generic configuration
406cec43e1SGabriel L. Somloitems are accessed with a selector value between 0x0000-0x7fff, and
416cec43e1SGabriel L. Somloarchitecture specific configuration items are accessed with a selector
426cec43e1SGabriel L. Somlovalue between 0x8000-0xffff.
436cec43e1SGabriel L. Somlo
446cec43e1SGabriel L. Somlo== Data Register ==
456cec43e1SGabriel L. Somlo
466cec43e1SGabriel L. Somlo* Read/Write (writes ignored as of QEMU v2.4)
476cec43e1SGabriel L. Somlo* Location: platform dependent (IOport [*] or MMIO)
486cec43e1SGabriel L. Somlo* Width: 8-bit (if IOport), 8/16/32/64-bit (if MMIO)
496cec43e1SGabriel L. Somlo* Endianness: string-preserving
506cec43e1SGabriel L. Somlo
516cec43e1SGabriel L. Somlo[*] On platforms where the data register is exposed as an IOport, its
526cec43e1SGabriel L. Somloport number will always be one greater than the port number of the
536cec43e1SGabriel L. Somloselector register. In other words, the two ports overlap, and can not
546cec43e1SGabriel L. Somlobe mapped separately.
556cec43e1SGabriel L. Somlo
566cec43e1SGabriel L. SomloThe data register allows access to an array of bytes for each firmware
576cec43e1SGabriel L. Somloconfiguration data item. The specific item is selected by writing to
586cec43e1SGabriel L. Somlothe selector register, as described above.
596cec43e1SGabriel L. Somlo
606cec43e1SGabriel L. SomloInitially following a write to the selector register, the data offset
616cec43e1SGabriel L. Somlowill be set to zero. Each successful access to the data register will
626cec43e1SGabriel L. Somloincrement the data offset by the appropriate access width.
636cec43e1SGabriel L. Somlo
646cec43e1SGabriel L. SomloEach firmware configuration item has a maximum length of data
656cec43e1SGabriel L. Somloassociated with the item. After the data offset has passed the
666cec43e1SGabriel L. Somloend of this maximum data length, then any reads will return a data
676cec43e1SGabriel L. Somlovalue of 0x00, and all writes will be ignored.
686cec43e1SGabriel L. Somlo
696cec43e1SGabriel L. SomloAn N-byte wide read of the data register will return the next available
706cec43e1SGabriel L. SomloN bytes of the selected firmware configuration item, as a substring, in
716cec43e1SGabriel L. Somloincreasing address order, similar to memcpy().
726cec43e1SGabriel L. Somlo
736cec43e1SGabriel L. Somlo== Register Locations ==
746cec43e1SGabriel L. Somlo
756cec43e1SGabriel L. Somlo=== x86, x86_64 Register Locations ===
766cec43e1SGabriel L. Somlo
776cec43e1SGabriel L. SomloSelector Register IOport: 0x510
786cec43e1SGabriel L. SomloData Register IOport:     0x511
796cec43e1SGabriel L. Somlo
806cec43e1SGabriel L. Somlo== Firmware Configuration Items ==
816cec43e1SGabriel L. Somlo
826cec43e1SGabriel L. Somlo=== Signature (Key 0x0000, FW_CFG_SIGNATURE) ===
836cec43e1SGabriel L. Somlo
846cec43e1SGabriel L. SomloThe presence of the fw_cfg selector and data registers can be verified
856cec43e1SGabriel L. Somloby selecting the "signature" item using key 0x0000 (FW_CFG_SIGNATURE),
866cec43e1SGabriel L. Somloand reading four bytes from the data register. If the fw_cfg device is
876cec43e1SGabriel L. Somlopresent, the four bytes read will contain the characters "QEMU".
886cec43e1SGabriel L. Somlo
896cec43e1SGabriel L. Somlo=== Revision (Key 0x0001, FW_CFG_ID) ===
906cec43e1SGabriel L. Somlo
916cec43e1SGabriel L. SomloA 32-bit little-endian unsigned int, this item is used as an interface
926cec43e1SGabriel L. Somlorevision number, and is currently set to 1 by QEMU when fw_cfg is
936cec43e1SGabriel L. Somloinitialized.
946cec43e1SGabriel L. Somlo
956cec43e1SGabriel L. Somlo=== File Directory (Key 0x0019, FW_CFG_FILE_DIR) ===
966cec43e1SGabriel L. Somlo
976cec43e1SGabriel L. SomloFirmware configuration items stored at selector keys 0x0020 or higher
986cec43e1SGabriel L. Somlo(FW_CFG_FILE_FIRST or higher) have an associated entry in a directory
996cec43e1SGabriel L. Somlostructure, which makes it easier for guest-side firmware to identify
1006cec43e1SGabriel L. Somloand retrieve them. The format of this file directory (from fw_cfg.h in
1016cec43e1SGabriel L. Somlothe QEMU source tree) is shown here, slightly annotated for clarity:
1026cec43e1SGabriel L. Somlo
1036cec43e1SGabriel L. Somlostruct FWCfgFiles {		/* the entire file directory fw_cfg item */
1046cec43e1SGabriel L. Somlo    uint32_t count;		/* number of entries, in big-endian format */
1056cec43e1SGabriel L. Somlo    struct FWCfgFile f[];	/* array of file entries, see below */
1066cec43e1SGabriel L. Somlo};
1076cec43e1SGabriel L. Somlo
1086cec43e1SGabriel L. Somlostruct FWCfgFile {		/* an individual file entry, 64 bytes total */
1096cec43e1SGabriel L. Somlo    uint32_t size;		/* size of referenced fw_cfg item, big-endian */
1106cec43e1SGabriel L. Somlo    uint16_t select;		/* selector key of fw_cfg item, big-endian */
1116cec43e1SGabriel L. Somlo    uint16_t reserved;
1126cec43e1SGabriel L. Somlo    char name[56];		/* fw_cfg item name, NUL-terminated ascii */
1136cec43e1SGabriel L. Somlo};
1146cec43e1SGabriel L. Somlo
1156cec43e1SGabriel L. Somlo=== All Other Data Items ===
1166cec43e1SGabriel L. Somlo
1176cec43e1SGabriel L. SomloPlease consult the QEMU source for the most up-to-date and authoritative
1186cec43e1SGabriel L. Somlolist of selector keys and their respective items' purpose and format.
1196cec43e1SGabriel L. Somlo
1206cec43e1SGabriel L. Somlo=== Ranges ===
1216cec43e1SGabriel L. Somlo
1226cec43e1SGabriel L. SomloTheoretically, there may be up to 0x4000 generic firmware configuration
1236cec43e1SGabriel L. Somloitems, and up to 0x4000 architecturally specific ones.
1246cec43e1SGabriel L. Somlo
1256cec43e1SGabriel L. SomloSelector Reg.    Range Usage
1266cec43e1SGabriel L. Somlo---------------  -----------
1276cec43e1SGabriel L. Somlo0x0000 - 0x3fff  Generic (0x0000 - 0x3fff, RO)
1286cec43e1SGabriel L. Somlo0x4000 - 0x7fff  Generic (0x0000 - 0x3fff, RW, ignored in QEMU v2.4+)
1296cec43e1SGabriel L. Somlo0x8000 - 0xbfff  Arch. Specific (0x0000 - 0x3fff, RO)
1306cec43e1SGabriel L. Somlo0xc000 - 0xffff  Arch. Specific (0x0000 - 0x3fff, RW, ignored in v2.4+)
1316cec43e1SGabriel L. Somlo
1326cec43e1SGabriel L. SomloIn practice, the number of allowed firmware configuration items is given
1336cec43e1SGabriel L. Somloby the value of FW_CFG_MAX_ENTRY (see fw_cfg.h).
1346cec43e1SGabriel L. Somlo
1356cec43e1SGabriel L. Somlo= Host-side API =
1366cec43e1SGabriel L. Somlo
1376cec43e1SGabriel L. SomloThe following functions are available to the QEMU programmer for adding
1386cec43e1SGabriel L. Somlodata to a fw_cfg device during guest initialization (see fw_cfg.h for
1396cec43e1SGabriel L. Somloeach function's complete prototype):
1406cec43e1SGabriel L. Somlo
1416cec43e1SGabriel L. Somlo== fw_cfg_add_bytes() ==
1426cec43e1SGabriel L. Somlo
1436cec43e1SGabriel L. SomloGiven a selector key value, starting pointer, and size, create an item
1446cec43e1SGabriel L. Somloas a raw "blob" of the given size, available by selecting the given key.
1456cec43e1SGabriel L. SomloThe data referenced by the starting pointer is only linked, NOT copied,
1466cec43e1SGabriel L. Somlointo the data structure of the fw_cfg device.
1476cec43e1SGabriel L. Somlo
1486cec43e1SGabriel L. Somlo== fw_cfg_add_string() ==
1496cec43e1SGabriel L. Somlo
1506cec43e1SGabriel L. SomloInstead of a starting pointer and size, this function accepts a pointer
1516cec43e1SGabriel L. Somloto a NUL-terminated ascii string, and inserts a newly allocated copy of
1526cec43e1SGabriel L. Somlothe string (including the NUL terminator) into the fw_cfg device data
1536cec43e1SGabriel L. Somlostructure.
1546cec43e1SGabriel L. Somlo
1556cec43e1SGabriel L. Somlo== fw_cfg_add_iXX() ==
1566cec43e1SGabriel L. Somlo
1576cec43e1SGabriel L. SomloInsert an XX-bit item, where XX may be 16, 32, or 64. These functions
1586cec43e1SGabriel L. Somlowill convert a 16-, 32-, or 64-bit integer to little-endian, then add
1596cec43e1SGabriel L. Somloa dynamically allocated copy of the appropriately sized item to fw_cfg
1606cec43e1SGabriel L. Somlounder the given selector key value.
1616cec43e1SGabriel L. Somlo
162*57c3d238SGabriel L. Somlo== fw_cfg_modify_iXX() ==
163*57c3d238SGabriel L. Somlo
164*57c3d238SGabriel L. SomloModify the value of an XX-bit item (where XX may be 16, 32, or 64).
165*57c3d238SGabriel L. SomloSimilarly to the corresponding fw_cfg_add_iXX() function set, convert
166*57c3d238SGabriel L. Somloa 16-, 32-, or 64-bit integer to little endian, create a dynamically
167*57c3d238SGabriel L. Somloallocated copy of the required size, and replace the existing item at
168*57c3d238SGabriel L. Somlothe given selector key value with the newly allocated one. The previous
169*57c3d238SGabriel L. Somloitem, assumed to have been allocated during an earlier call to
170*57c3d238SGabriel L. Somlofw_cfg_add_iXX() or fw_cfg_modify_iXX() (of the same width XX), is freed
171*57c3d238SGabriel L. Somlobefore the function returns.
172*57c3d238SGabriel L. Somlo
1736cec43e1SGabriel L. Somlo== fw_cfg_add_file() ==
1746cec43e1SGabriel L. Somlo
1756cec43e1SGabriel L. SomloGiven a filename (i.e., fw_cfg item name), starting pointer, and size,
1766cec43e1SGabriel L. Somlocreate an item as a raw "blob" of the given size. Unlike fw_cfg_add_bytes()
1776cec43e1SGabriel L. Somloabove, the next available selector key (above 0x0020, FW_CFG_FILE_FIRST)
1786cec43e1SGabriel L. Somlowill be used, and a new entry will be added to the file directory structure
1796cec43e1SGabriel L. Somlo(at key 0x0019), containing the item name, blob size, and automatically
1806cec43e1SGabriel L. Somloassigned selector key value. The data referenced by the starting pointer
1816cec43e1SGabriel L. Somlois only linked, NOT copied, into the fw_cfg data structure.
1826cec43e1SGabriel L. Somlo
1836cec43e1SGabriel L. Somlo== fw_cfg_add_file_callback() ==
1846cec43e1SGabriel L. Somlo
1856cec43e1SGabriel L. SomloLike fw_cfg_add_file(), but additionally sets pointers to a callback
1866cec43e1SGabriel L. Somlofunction (and opaque argument), which will be executed host-side by
1876cec43e1SGabriel L. SomloQEMU each time a byte is read by the guest from this particular item.
1886cec43e1SGabriel L. Somlo
1896cec43e1SGabriel L. SomloNOTE: The callback function is given the opaque argument set by
1906cec43e1SGabriel L. Somlofw_cfg_add_file_callback(), but also the current data offset,
1916cec43e1SGabriel L. Somloallowing it the option of only acting upon specific offset values
1926cec43e1SGabriel L. Somlo(e.g., 0, before the first data byte of the selected item is
1936cec43e1SGabriel L. Somloreturned to the guest).
1946cec43e1SGabriel L. Somlo
1956cec43e1SGabriel L. Somlo== fw_cfg_modify_file() ==
1966cec43e1SGabriel L. Somlo
1976cec43e1SGabriel L. SomloGiven a filename (i.e., fw_cfg item name), starting pointer, and size,
1986cec43e1SGabriel L. Somlocompletely replace the configuration item referenced by the given item
1996cec43e1SGabriel L. Somloname with the new given blob. If an existing blob is found, its
2006cec43e1SGabriel L. Somlocallback information is removed, and a pointer to the old data is
2016cec43e1SGabriel L. Somloreturned to allow the caller to free it, helping avoid memory leaks.
2026cec43e1SGabriel L. SomloIf a configuration item does not already exist under the given item
2036cec43e1SGabriel L. Somloname, a new item will be created as with fw_cfg_add_file(), and NULL
2046cec43e1SGabriel L. Somlois returned to the caller. In any case, the data referenced by the
2056cec43e1SGabriel L. Somlostarting pointer is only linked, NOT copied, into the fw_cfg data
2066cec43e1SGabriel L. Somlostructure.
2076cec43e1SGabriel L. Somlo
2086cec43e1SGabriel L. Somlo== fw_cfg_add_callback() ==
2096cec43e1SGabriel L. Somlo
2106cec43e1SGabriel L. SomloLike fw_cfg_add_bytes(), but additionally sets pointers to a callback
2116cec43e1SGabriel L. Somlofunction (and opaque argument), which will be executed host-side by
2126cec43e1SGabriel L. SomloQEMU each time a guest-side write operation to this particular item
2136cec43e1SGabriel L. Somlocompletes fully overwriting the item's data.
2146cec43e1SGabriel L. Somlo
2156cec43e1SGabriel L. SomloNOTE: This function is deprecated, and will be completely removed
2166cec43e1SGabriel L. Somlostarting with QEMU v2.4.
21781b2b810SGabriel L. Somlo
21881b2b810SGabriel L. Somlo== Externally Provided Items ==
21981b2b810SGabriel L. Somlo
22081b2b810SGabriel L. SomloAs of v2.4, "file" fw_cfg items (i.e., items with selector keys above
22181b2b810SGabriel L. SomloFW_CFG_FILE_FIRST, and with a corresponding entry in the fw_cfg file
22281b2b810SGabriel L. Somlodirectory structure) may be inserted via the QEMU command line, using
22381b2b810SGabriel L. Somlothe following syntax:
22481b2b810SGabriel L. Somlo
22581b2b810SGabriel L. Somlo    -fw_cfg [name=]<item_name>,file=<path>
22681b2b810SGabriel L. Somlo
22781b2b810SGabriel L. Somlowhere <item_name> is the fw_cfg item name, and <path> is the location
22881b2b810SGabriel L. Somloon the host file system of a file containing the data to be inserted.
22981b2b810SGabriel L. Somlo
2306407d76eSGabriel L. SomloSmall enough items may be provided directly as strings on the command
2316407d76eSGabriel L. Somloline, using the syntax:
2326407d76eSGabriel L. Somlo
2336407d76eSGabriel L. Somlo    -fw_cfg [name=]<item_name>,string=<string>
2346407d76eSGabriel L. Somlo
2356407d76eSGabriel L. SomloThe terminating NUL character of the content <string> will NOT be
2366407d76eSGabriel L. Somloincluded as part of the fw_cfg item data, which is consistent with
2376407d76eSGabriel L. Somlothe absence of a NUL terminator for items inserted via the file option.
2386407d76eSGabriel L. Somlo
2396407d76eSGabriel L. SomloBoth <item_name> and, if applicable, the content <string> are passed
2406407d76eSGabriel L. Somlothrough by QEMU without any interpretation, expansion, or further
2416407d76eSGabriel L. Somloprocessing. Any such processing (potentially performed e.g., by the shell)
2426407d76eSGabriel L. Somlois outside of QEMU's responsibility; as such, using plain ASCII characters
2436407d76eSGabriel L. Somlois recommended.
2446407d76eSGabriel L. Somlo
24581b2b810SGabriel L. SomloNOTE: Users *SHOULD* choose item names beginning with the prefix "opt/"
24681b2b810SGabriel L. Somlowhen using the "-fw_cfg" command line option, to avoid conflicting with
24781b2b810SGabriel L. Somloitem names used internally by QEMU. For instance:
24881b2b810SGabriel L. Somlo
24981b2b810SGabriel L. Somlo    -fw_cfg name=opt/my_item_name,file=./my_blob.bin
25081b2b810SGabriel L. Somlo
25181b2b810SGabriel L. SomloSimilarly, QEMU developers *SHOULD NOT* use item names prefixed with
25281b2b810SGabriel L. Somlo"opt/" when inserting items programmatically, e.g. via fw_cfg_add_file().
253