./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
145 / 159
91.2%
Branches:
103 / 136
75.7%
Functions:
11 / 11
100.0%

kfsw-services/src/ftp/ftp_transfer.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <string.h>
6
7 #include <zephyr/fs/fs.h>
8 #include <zephyr/sys/crc.h>
9 #include <zephyr/sys/util.h>
10
11 #if CONFIG_KFSW_FWU_STANDARD
12 #include <kfsw/services/fwu.h>
13 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_FTP
14 #include <kfsw/services/log.h>
15 #endif
16
17 #include "ftp_link.h"
18
19 /*
20 * The send and receive loops, shared by the client and the server.
21 */
22
23 32 static int write_all(struct fs_file_t *file, const uint8_t *data, size_t size)
24 {
25 32 size_t offset = 0U;
26
27
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 32 times.
64 while (offset < size) {
28 32 ssize_t written = fs_write(file, &data[offset], size - offset);
29
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (written < 0) {
31 ✗ return (int)written;
32 }
33
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (written == 0) {
34 ✗ return -EIO;
35 }
36 32 offset += (size_t)written;
37 }
38 32 return 0;
39 }
40
41 /*
42 * Check transfer ID, offset, size and CRC before advancing the write.
43 */
44 42 static bool data_message_is_valid(const struct kfsw_ftp_transfer *transfer,
45 const struct kfsw_ftp_message *message)
46 {
47 82 return (message->opcode == transfer->data_opcode) &&
48
3/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
40 (message->request_id == transfer->request_id) && (message->status == 0U) &&
49
3/6
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
38 (message->flags == 0U) && (message->path_size == 0U) && (message->data_size != 0U) &&
50
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2 times.
38 (message->offset == transfer->offset) &&
51
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 (message->total_size == transfer->total_size) &&
52
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
118 (message->crc32 == transfer->crc32) &&
53
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 2 times.
36 (message->data_size <= transfer->total_size - transfer->offset);
54 }
55
56 20 int kfsw_ftp_transfer_open_source(struct kfsw_ftp_transfer *transfer, const char *source_path)
57 {
58 int result;
59
60
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if ((transfer == NULL) || (source_path == NULL)) {
61 ✗ return -EINVAL;
62 }
63 20 fs_file_t_init(&transfer->file);
64 20 result = fs_open(&transfer->file, source_path, FS_O_READ);
65
3/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 2 times.
20 if ((result != 0) || (transfer->offset == 0U)) {
66 18 return result;
67 }
68
69 /* Continuing a transfer: skip what the far side already holds. */
70 2 result = fs_seek(&transfer->file, (off_t)transfer->offset, FS_SEEK_SET);
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
72 ✗ (void)fs_close(&transfer->file);
73 }
74 2 return result;
75 }
76
77 20 int kfsw_ftp_transfer_send(struct kfsw_ftp_transfer *transfer)
78 {
79 int close_result;
80 20 int result = 0;
81
82
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if ((transfer == NULL) || (transfer->workspace == NULL)) {
83 ✗ return -EINVAL;
84 }
85
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 18 times.
36 while ((result == 0) && (transfer->offset < transfer->total_size)) {
86 /* The runtime chunk size can only be smaller than the buffer. */
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 ssize_t bytes_read = fs_read(
88 16 &transfer->file, transfer->workspace->chunk,
89 16 MIN((size_t)kfsw_ftp_get_chunk_size(), sizeof(transfer->workspace->chunk)));
90 16 struct kfsw_ftp_message data_message = {
91 16 .opcode = transfer->data_opcode,
92 16 .request_id = transfer->request_id,
93 16 .offset = transfer->offset,
94 16 .total_size = transfer->total_size,
95 16 .crc32 = transfer->crc32,
96 16 .data = transfer->workspace->chunk,
97 };
98
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (bytes_read <= 0) {
100
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ result = (bytes_read < 0) ? (int)bytes_read : -EIO;
101 ✗ break;
102 }
103 16 data_message.data_size = (uint16_t)bytes_read;
104 16 result = kfsw_ftp_link_send(transfer->link, &data_message);
105
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 transfer->offset += (result == 0) ? (uint32_t)bytes_read : 0U;
106 }
107 20 close_result = fs_close(&transfer->file);
108
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 return (result != 0) ? result : close_result;
109 }
110
111 36 int kfsw_ftp_transfer_open_sink(struct kfsw_ftp_transfer *transfer, const char *temporary_path)
112 {
113 int result;
114
115
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
36 if ((transfer == NULL) || (temporary_path == NULL)) {
116 ✗ return -EINVAL;
117 }
118
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 8 times.
36 if (transfer->offset == 0U) {
119 28 (void)fs_unlink(temporary_path);
120 28 fs_file_t_init(&transfer->file);
121 28 return fs_open(&transfer->file, temporary_path,
122 FS_O_CREATE | FS_O_WRITE | FS_O_TRUNC);
123 }
124
125 /* Continuing a partial: keep what is there and write past it. */
126 8 fs_file_t_init(&transfer->file);
127 8 result = fs_open(&transfer->file, temporary_path, FS_O_WRITE);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (result != 0) {
129 ✗ return result;
130 }
131 8 result = fs_seek(&transfer->file, (off_t)transfer->offset, FS_SEEK_SET);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (result != 0) {
133 ✗ (void)fs_close(&transfer->file);
134 }
135 8 return result;
136 }
137
138 #if CONFIG_KFSW_FWU_STANDARD
139 /* The single FTP server keeps failed cleanup alive after its connection ends. */
140 static struct kfsw_fwu_handle pending_firmware_cleanup;
141
142 22 static int abort_firmware(const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
143 {
144 22 int result = kfsw_fwu_session_abort(handle, status);
145
146
5/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
22 if (result == 0 || result == -ESTALE || result == -EACCES) {
147
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (pending_firmware_cleanup.generation == handle->generation) {
148 8 pending_firmware_cleanup.generation = 0U;
149 }
150 } else {
151 12 pending_firmware_cleanup = *handle;
152 }
153 22 return result;
154 }
155
156 24 static int retry_firmware_cleanup(uint16_t peer)
157 {
158 struct kfsw_fwu_status status;
159 24 const struct kfsw_fwu_handle handle = pending_firmware_cleanup;
160 int result;
161
162
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 10 times.
24 if (handle.generation == 0U || handle.owner.peer != peer) {
163 14 return 0;
164 }
165 10 result = abort_firmware(&handle, &status);
166
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
10 return result == -ESTALE || result == -EACCES ? 0 : result;
167 }
168
169 42 bool kfsw_ftp_path_is_firmware(const uint8_t *path, uint16_t path_size)
170 {
171 static const char reserved[] = CONFIG_KFSW_FTP_FIRMWARE_PATH;
172 42 const size_t reserved_size = sizeof(reserved) - 1U;
173
174 /* The path on the wire is not terminated, so compare it by length. */
175
4/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 30 times.
42 if ((path == NULL) || (path_size == 0U)) {
176 12 return false;
177 }
178
179 /* A leading separator is how the same path is often written, and the
180 * transfer root makes both mean the same file. */
181
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 24 times.
30 if (path[0] == (uint8_t)'/') {
182 6 path++;
183 6 path_size--;
184 }
185
186
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 16 times.
30 if (path_size != reserved_size) {
187 14 return false;
188 }
189
190 16 return memcmp(path, reserved, reserved_size) == 0;
191 }
192
193 24 int kfsw_ftp_transfer_open_firmware_sink(struct kfsw_ftp_transfer *transfer)
194 {
195 struct kfsw_fwu_status status;
196 24 struct kfsw_fwu_handle acquired = {0};
197 24 struct kfsw_fwu_image image = {0};
198 24 struct kfsw_fwu_owner owner = {.kind = KFSW_FWU_OWNER_FTP};
199 int result;
200
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (transfer == NULL) {
202 ✗ return -EINVAL;
203 }
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (transfer->link == NULL) {
206 ✗ return -EINVAL;
207 }
208 24 owner.peer = transfer->link->peer;
209 24 result = retry_firmware_cleanup(owner.peer);
210
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22 times.
24 if (result != 0) {
211 2 return result;
212 }
213 22 image.size = transfer->total_size;
214 22 image.crc32 = transfer->crc32;
215 22 result = kfsw_fwu_session_begin(&owner, &image, 0U, &acquired, &status);
216
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 14 times.
22 if (result != 0) {
217
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (acquired.generation != 0U) {
218 4 int cleanup = abort_firmware(&acquired, &status);
219
220
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (cleanup != 0) {
221 4 kfsw_log_error("Firmware upload cleanup failed (%d)", cleanup);
222 }
223 }
224 8 return result;
225 }
226
227 14 transfer->firmware_handle = acquired;
228 14 transfer->firmware = true;
229 14 return 0;
230 }
231 #endif /* CONFIG_KFSW_FWU_STANDARD */
232
233 38 int kfsw_ftp_transfer_receive(struct kfsw_ftp_transfer *transfer)
234 {
235 38 int result = 0;
236
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (transfer == NULL) {
238 ✗ return -EINVAL;
239 }
240
4/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 16 times.
80 while ((result == 0) && (transfer->offset < transfer->total_size)) {
241 struct kfsw_ftp_link_frame frame;
242
243 56 result = kfsw_ftp_link_receive(transfer->link, &frame);
244
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 42 times.
56 if (result != 0) {
245 14 break;
246 }
247
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 34 times.
42 if (!data_message_is_valid(transfer, &frame.message)) {
248 8 result = -EBADMSG;
249 #if CONFIG_KFSW_FWU_STANDARD
250
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 } else if (transfer->firmware) {
251 struct kfsw_fwu_status status;
252
253 2 result = kfsw_fwu_session_write(&transfer->firmware_handle,
254 2 transfer->offset, frame.message.data,
255 2 frame.message.data_size, &status);
256 #endif
257 } else {
258 32 result = write_all(&transfer->file, frame.message.data,
259 32 frame.message.data_size);
260 }
261
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 8 times.
42 if (result == 0) {
262 34 transfer->actual_crc32 =
263 34 crc32_ieee_update(transfer->actual_crc32, frame.message.data,
264 34 frame.message.data_size);
265 34 transfer->offset += frame.message.data_size;
266 }
267 42 kfsw_ftp_link_release(&frame);
268 }
269 38 return result;
270 }
271
272 30 int kfsw_ftp_transfer_finish(struct kfsw_ftp_transfer *transfer, const char *target_path,
273 const char *temporary_path, int result)
274 {
275 int close_result;
276 int sync_result;
277
278
3/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30 times.
30 if ((transfer == NULL) || (target_path == NULL) || (temporary_path == NULL)) {
279 ✗ return -EINVAL;
280 }
281
282 #if CONFIG_KFSW_FWU_STANDARD
283
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 if (transfer->firmware) {
284 struct kfsw_fwu_status status;
285
286 /* No file was opened; the update service handles a rejected image. */
287
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (result == 0) {
288 2 result = kfsw_fwu_session_verify(&transfer->firmware_handle, &status);
289
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (result == 0) {
290 2 result = kfsw_fwu_session_activate(&transfer->firmware_handle,
291 &status);
292 }
293 }
294
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (result != 0) {
295 8 int cleanup = abort_firmware(&transfer->firmware_handle, &status);
296
297
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (cleanup != 0) {
298 8 kfsw_log_error("Firmware upload cleanup failed (%d)", cleanup);
299 }
300 }
301 10 return result;
302 }
303 #endif
304
305 /* Synced even after a failure, so an interrupted upload leaves on disk
306 * exactly what the next attempt can continue from.
307 */
308 20 sync_result = fs_sync(&transfer->file);
309
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (result == 0) {
310 10 result = sync_result;
311 }
312 20 close_result = fs_close(&transfer->file);
313
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (result == 0) {
314 10 result = close_result;
315 }
316
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (result == 0) {
317 /* Removes the partial itself when the content does not match. */
318 10 result = kfsw_ftp_commit_temporary(target_path, temporary_path, transfer->offset,
319 transfer->actual_crc32, transfer->total_size,
320 transfer->crc32);
321 }
322 20 return result;
323 }
324