xref: /aosp_15_r20/external/crosvm/virtio_sys/bindgen.sh (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1#!/usr/bin/env bash
2# Copyright 2022 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Regenerate virtio_sys bindgen bindings.
7
8set -euo pipefail
9cd "$(dirname "${BASH_SOURCE[0]}")/.."
10
11source tools/impl/bindgen-common.sh
12
13bindgen_generate \
14    --allowlist-type='vhost_.*' \
15    --allowlist-var='VHOST_.*' \
16    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/vhost.h" \
17    -- \
18    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
19    | replace_linux_int_types \
20    > virtio_sys/src/vhost.rs
21
22VIRTIO_CONFIG_EXTRA="// Added by virtio_sys/bindgen.sh
23pub const VIRTIO_CONFIG_S_SUSPEND: u32 = 16;
24pub const VIRTIO_F_SUSPEND: u32 = 42;"
25
26bindgen_generate \
27    --raw-line "${VIRTIO_CONFIG_EXTRA}" \
28    --allowlist-var='VIRTIO_.*' \
29    --allowlist-type='virtio_.*' \
30    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_config.h" \
31    -- \
32    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
33    | replace_linux_int_types \
34    > virtio_sys/src/virtio_config.rs
35
36VIRTIO_FS_EXTRA="// Added by virtio_sys/bindgen.sh
37use data_model::Le32;
38use zerocopy::AsBytes;
39use zerocopy::FromBytes;
40use zerocopy::FromZeroes;"
41
42bindgen_generate \
43    --raw-line "${VIRTIO_FS_EXTRA}" \
44    --allowlist-var='VIRTIO_FS_.*' \
45    --allowlist-type='virtio_fs_.*' \
46    --with-derive-custom "virtio_fs_config=FromZeroes,FromBytes,AsBytes" \
47    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_fs.h" \
48    -- \
49    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
50    | replace_linux_int_types \
51    | replace_linux_endian_types \
52    > virtio_sys/src/virtio_fs.rs
53
54VIRTIO_IDS_EXTRAS="
55//! This file defines virtio device IDs. IDs with large values (counting down
56//! from 63) are nonstandard and not defined by the virtio specification.
57
58// Added by virtio_sys/bindgen.sh - do not edit the generated file.
59// TODO(b/236144983): Fix this id when an official virtio-id is assigned to this device.
60pub const VIRTIO_ID_PVCLOCK: u32 = 61;
61// TODO: Remove this once the ID is included in the Linux headers.
62pub const VIRTIO_ID_MEDIA: u32 = 48;
63"
64
65bindgen_generate \
66    --raw-line "${VIRTIO_IDS_EXTRAS}" \
67    --allowlist-var='VIRTIO_ID_.*' \
68    --allowlist-type='virtio_.*' \
69    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_ids.h" \
70    -- \
71    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
72    | replace_linux_int_types \
73    | rustfmt \
74    > virtio_sys/src/virtio_ids.rs
75
76VIRTIO_NET_EXTRA="// Added by virtio_sys/bindgen.sh
77use zerocopy::AsBytes;
78use zerocopy::FromBytes;
79use zerocopy::FromZeroes;"
80
81bindgen_generate \
82    --raw-line "${VIRTIO_NET_EXTRA}" \
83    --allowlist-var='VIRTIO_NET_.*' \
84    --allowlist-type='virtio_net_.*' \
85    --blocklist-type='virtio_net_ctrl_mac' \
86    --with-derive-custom "virtio_net_hdr=FromZeroes,FromBytes,AsBytes" \
87    --with-derive-custom "virtio_net_hdr_mrg_rxbuf=FromZeroes,FromBytes,AsBytes" \
88    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_net.h" \
89    -- \
90    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
91    | replace_linux_int_types \
92    > virtio_sys/src/virtio_net.rs
93
94bindgen_generate \
95    --allowlist-var='VRING_.*' \
96    --allowlist-var='VIRTIO_RING_.*' \
97    --allowlist-type='vring.*' \
98    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_ring.h" \
99    -- \
100    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
101    | replace_linux_int_types \
102    > virtio_sys/src/virtio_ring.rs
103
104bindgen_generate \
105    --allowlist-var='VIRTIO_.*' \
106    --allowlist-type='virtio_.*' \
107    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_mmio.h" \
108    -- \
109    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
110    | replace_linux_int_types \
111    > virtio_sys/src/virtio_mmio.rs
112
113VIRTIO_VSOCK_EXTRA="// Added by virtio_sys/bindgen.sh
114use data_model::Le16;
115use data_model::Le32;
116use data_model::Le64;
117use zerocopy::AsBytes;"
118
119bindgen_generate \
120    --raw-line "${VIRTIO_VSOCK_EXTRA}" \
121    --allowlist-var='VIRTIO_VSOCK_.*' \
122    --allowlist-type='virtio_vsock_.*' \
123    --with-derive-custom "virtio_vsock_event=AsBytes" \
124    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_vsock.h" \
125    -- \
126    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
127    | replace_linux_int_types \
128    | replace_linux_endian_types \
129    > virtio_sys/src/virtio_vsock.rs
130
131VIRTIO_SCSI_EXTRA="// Added by virtio_sys/bindgen.sh
132use zerocopy::AsBytes;
133use zerocopy::FromBytes;
134use zerocopy::FromZeroes;"
135
136bindgen_generate \
137    --raw-line "${VIRTIO_SCSI_EXTRA}" \
138    --allowlist-var='VIRTIO_SCSI_.*' \
139    --allowlist-type='virtio_scsi_.*' \
140    --blocklist-type='virtio_scsi_cmd_req_pi' \
141    --with-derive-custom "virtio_scsi_config=FromZeroes,FromBytes,AsBytes" \
142    --with-derive-custom "virtio_scsi_cmd_req=FromZeroes,FromBytes,AsBytes" \
143    --with-derive-custom "virtio_scsi_cmd_resp=FromZeroes,FromBytes,AsBytes" \
144    --with-derive-custom "virtio_scsi_ctrl_tmf_req=FromZeroes,FromBytes,AsBytes" \
145    --with-derive-custom "virtio_scsi_ctrl_an_req=FromZeroes,FromBytes,AsBytes" \
146    --with-derive-custom "virtio_scsi_ctrl_tmf_resp=FromZeroes,FromBytes,AsBytes" \
147    --with-derive-custom "virtio_scsi_ctrl_an_resp=FromZeroes,FromBytes,AsBytes" \
148    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_scsi.h" \
149    -- \
150    -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \
151    | replace_linux_int_types \
152    | replace_linux_endian_types \
153    > virtio_sys/src/virtio_scsi.rs
154