1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This file is part of fpgad, an application to manage FPGA subsystem together with device-tree and kernel modules.
//
// Copyright 2025 Canonical Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only
//
// fpgad is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3, as published by the Free Software Foundation.
//
// fpgad is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
//! Load command implementation for the FPGA CLI.
//!
//! This module handles the loading of FPGA bitstreams and device tree overlays through
//! the fpgad daemon's DBus interface. It provides functionality to:
//! - Load FPGA bitstreams onto FPGA devices
//! - Apply device tree overlays
//! - Automatically detect and use default FPGA devices when not specified
//!
//! The module communicates with the fpgad daemon via DBus to perform these privileged
//! operations on the FPGA subsystem.
//!
//! For information on [Device Handles], [Overlay Handles], and [Error Handling],
//! see the [Common Concepts](../index.html#common-concepts) section in the main CLI documentation.
//!
//! [Device Handles]: ../index.html#device-handles
//! [Overlay Handles]: ../index.html#overlay-handles
//! [Error Handling]: ../index.html#error-handling
use crateLoadSubcommand;
use cratecontrol_proxy;
use crate;
use path;
use Connection;
/// Sanitizes and converts a file path string to an absolute path.
///
/// # Arguments
///
/// * `in_str` - The input path string to sanitize
///
/// # Returns: `Result<String, zbus::Error>`
/// * `String` - Absolute path string resolved from the input
/// * `zbus::Error::Failure` - If the path cannot be resolved to an absolute path
/// Sends the DBus command to load a bitstream onto an FPGA device.
///
/// Communicates with the fpgad daemon via DBus to write a bitstream file to the
/// specified FPGA device. The bitstream configures the FPGA's logic fabric.
///
/// # Arguments
///
/// * `platform_str` - Platform identifier (empty string for auto-detection)
/// * `device_handle` - The [device handle](../index.html#device-handles) (e.g., "fpga0")
/// * `file_path` - Absolute path to the bitstream file
/// * `firmware_lookup_path` - Optional firmware lookup path (empty string for default)
///
/// # Returns: `Result<String, zbus::Error>`
/// * `Ok(String)` - Success message from the daemon
/// * `Err(zbus::Error)` - DBus communication error or FpgadError.
/// See [Error Handling](../index.html#error-handling) for details.
async
/// Sends the DBus command to apply a device tree overlay.
///
/// Communicates with the fpgad daemon via DBus to load a device tree overlay file.
/// Overlays describe hardware configuration and interfaces for the FPGA peripherals.
///
/// # Arguments
///
/// * `platform` - Platform identifier string
/// * `file_path` - Absolute path to the overlay file (.dtbo)
/// * `overlay_handle` - [Overlay handle](../index.html#overlay-handles) for the overlay directory in configfs
/// * `firmware_lookup_path` - Optional firmware lookup path (empty string for default)
///
/// # Returns: `Result<String, zbus::Error>`
/// * `Ok(String)` - Success message from the daemon
/// * `Err(zbus::Error)` - DBus communication error or FpgadError.
/// See [Error Handling](../index.html#error-handling) for details.
async
/// Applies a device tree overlay with automatic platform and handle detection.
///
/// This function handles the logic for determining the appropriate platform and overlay
/// handle based on what the user has provided. It supports four scenarios:
/// 1. Both device and overlay handles provided - use both as-is
/// 2. Only device handle provided - use device name as overlay handle
/// 3. Only overlay handle provided - auto-detect first platform
/// 4. Neither provided - auto-detect both from first available device
///
/// # Arguments
///
/// * `dev_handle` - Optional [device handle](../index.html#device-handles) (e.g., "fpga0")
/// * `file_path` - Path to the overlay file (.dtbo)
/// * `overlay_handle` - Optional [overlay handle](../index.html#overlay-handles) for the overlay directory
///
/// # Returns: `Result<String, zbus::Error>`
/// * `Ok(String)` - Success message from the daemon
/// * `Err(zbus::Error)` - DBus communication error, device detection failure, or FpgadError.
/// See [Error Handling](../index.html#error-handling) for details.
async
/// Loads a bitstream onto an FPGA device with automatic device detection.
///
/// If no device handle is provided, this function automatically detects and uses
/// the first available FPGA device in the system.
///
/// # Arguments
///
/// * `device_handle` - Optional [device handle](../index.html#device-handles) (e.g., "fpga0")
/// * `file_path` - Path to the bitstream file
///
/// # Returns: `Result<String, zbus::Error>`
/// * `Ok(String)` - Success message from the daemon
/// * `Err(zbus::Error)` - DBus communication error, device detection failure, or FpgadError.
/// See [Error Handling](../index.html#error-handling) for details.
async
/// Main handler for the load command.
///
/// Dispatches to the appropriate load function based on the subcommand type
/// (overlay or bitstream). This is the entry point called by the CLI's main
/// function when a load command is issued.
///
/// # Arguments
///
/// * `dev_handle` - Optional [device handle](../index.html#device-handles)
/// * `sub_command` - The load subcommand specifying what to load (overlay or bitstream)
///
/// # Returns: `Result<String, zbus::Error>`
/// * `Ok(String)` - Success message from the operation
/// * `Err(zbus::Error)` - DBus communication error, operation failure, or FpgadError.
/// See [Error Handling](../index.html#error-handling) for details.
pub async