summaryrefslogtreecommitdiff
path: root/src/st-util/semihosting.c
blob: 8e9828ce8e5845612f10cf93a89a2972defea6dd (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <stlink.h>
#include "semihosting.h"

#include <logging.h>
#include <read_write.h>

static int32_t mem_read_u8(stlink_t *sl, uint32_t addr, uint8_t *data) {
    int32_t offset = addr % 4;
    int32_t len = 4;

    if (sl == NULL || data == NULL) { return (-1); }

    // read address and length must be aligned
    if (stlink_read_mem32(sl, addr - offset, len) != 0) { return (-1); }

    *data = sl->q_buf[offset];
    return (0);
}

#ifdef UNUSED
static int32_t mem_read_u16(stlink_t *sl, uint32_t addr, uint16_t *data) {
    int32_t offset = addr % 4;
    int32_t len = (offset > 2 ? 8 : 4);

    if (sl == NULL || data == NULL) { return (-1); }

    // read address and length must be aligned
    if (stlink_read_mem32(sl, addr - offset, len) != 0) { return (-1); }

    memcpy(data, &sl->q_buf[offset], sizeof(*data));
    return (0);
}

static int32_t mem_read_u32(stlink_t *sl, uint32_t addr, uint32_t *data) {
    int32_t offset = addr % 4;
    int32_t len = (offset > 0 ? 8 : 4);

    if (sl == NULL || data == NULL) { return (-1); }

    // read address and length must be aligned
    if (stlink_read_mem32(sl, addr - offset, len) != 0) { return (-1); }

    memcpy(data, &sl->q_buf[offset], sizeof(*data));
    return (0);
}
#endif

static int32_t mem_read(stlink_t *sl, uint32_t addr, void *data, uint16_t len) {
    int32_t offset = addr % 4;
    int32_t read_len = len + offset;

    if (sl == NULL || data == NULL) { return (-1); }

    // align read size
    if ((read_len % 4) != 0) { read_len += 4 - (read_len % 4); }

    // address and length must be aligned
    if (stlink_read_mem32(sl, addr - offset, read_len) != 0) { return (-1); }

    memcpy(data, &sl->q_buf[offset], len);
    return (0);
}

static int32_t mem_write(stlink_t *sl, uint32_t addr, void *data, uint16_t len) {
    /* Note: this function can write more than it is asked to!
     * If addr is not an even 32 bit boundary, or len is not a multiple of 4.
     * If only 32 bit values can be written to the target, then this function should read
     * the target memory at the start and end of the buffer where it will write more that
     * the requested bytes. (perhaps reading the whole area is faster??).
     * If 16 and 8 bit writes are available, then they could be used instead.
     * Just return when the length is zero avoiding unneeded work. */
    if (len == 0) { return (0); }

    int32_t offset = addr % 4;
    int32_t write_len = len + offset;

    if (sl == NULL || data == NULL) { return (-1); }

    // align read size
    if ((write_len % 4) != 0) { write_len += 4 - (write_len % 4); }

    memcpy(&sl->q_buf[offset], data, len);

    // address and length must be aligned
    if (stlink_write_mem32(sl, addr - offset, write_len) != 0) { return (-1); }

    return (0);
}

/* For the SYS_WRITE0 call, we don't know the size of the null-terminated buffer
 * in the target memory. Instead of reading one byte at a time, we read by
 * chunks of WRITE0_BUFFER_SIZE bytes.
 */
#define WRITE0_BUFFER_SIZE 64

/* Define a maximum size for buffers transmitted by semihosting. There is no
 * limit in the ARM specification but this is a safety net.
 * We remove 4 byte from Q_BUF_LEN to handle alignment correction.
 */
#define MAX_BUFFER_SIZE (Q_BUF_LEN - 4)

/* Flags for Open syscall */

#ifndef O_BINARY
#define O_BINARY 0
#endif

static int32_t open_mode_flags[12] = {
    O_RDONLY,
    O_RDONLY | O_BINARY,
    O_RDWR,
    O_RDWR   | O_BINARY,
    O_WRONLY | O_CREAT | O_TRUNC,
    O_WRONLY | O_CREAT | O_TRUNC  | O_BINARY,
    O_RDWR   | O_CREAT | O_TRUNC,
    O_RDWR   | O_CREAT | O_TRUNC  | O_BINARY,
    O_WRONLY | O_CREAT | O_APPEND,
    O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
    O_RDWR   | O_CREAT | O_APPEND,
    O_RDWR   | O_CREAT | O_APPEND | O_BINARY
};

static int32_t saved_errno = 0;

int32_t do_semihosting (stlink_t *sl, uint32_t r0, uint32_t r1, uint32_t *ret) {

    if (sl == NULL || ret == NULL) { return (-1); }

    DLOG("Do semihosting R0=0x%08x R1=0x%08x\n", r0, r1);

    switch (r0) {
    case SEMIHOST_SYS_OPEN:
    {
        uint32_t args[3];
        uint32_t name_address;
        uint32_t mode;
        uint32_t name_len;
        char     *name;

        if (mem_read(sl, r1, args, sizeof(args)) != 0) {
            DLOG("Semihosting SYS_OPEN error: cannot read args from target memory\n");
            *ret = -1;
            return (-1);
        }

        name_address = args[0];
        mode         = args[1];
        name_len     = args[2];

        if (mode > 12) {
            /* Invalid mode */
            DLOG("Semihosting SYS_OPEN error: invalid mode %d\n", mode);
            *ret = -1;
            return (-1);
        }

        /* Add the trailing zero that is not counted in the length argument (see
         * ARM semihosting specification)
         */
        name_len += 1;

        if (name_len > MAX_BUFFER_SIZE) {
            DLOG("Semihosting SYS_OPEN error: name buffer size is too big %d\n", name_len);
            *ret = -1;
            return (-1);
        }

        name = malloc(name_len);

        if (name == NULL) {
            DLOG("Semihosting SYS_OPEN error: cannot allocate name buffer\n");
            *ret = -1;
            return (-1);
        }

        if (mem_read(sl, name_address, name, name_len) != 0) {
            free(name);
            *ret = -1;
            DLOG("Semihosting SYS_OPEN error: cannot read name from target memory\n");
            return (-1);
        }

        DLOG("Semihosting: open('%s', (SH open mode)%d, 0644)\n", name, mode);

        *ret = (uint32_t)open(name, open_mode_flags[mode], 0644);
        saved_errno = errno;

        DLOG("Semihosting: return %d\n", *ret);

        free(name);
        break;
    }
    case SEMIHOST_SYS_CLOSE:
    {
        uint32_t args[1];
        int32_t fd;

        if (mem_read(sl, r1, args, sizeof(args)) != 0) {
            DLOG("Semihosting SYS_CLOSE error: cannot read args from target memory\n");
            *ret = -1;
            return (-1);
        }

        fd = (int32_t)args[0];

        DLOG("Semihosting: close(%d)\n", fd);

        *ret = (uint32_t)close(fd);
        saved_errno = errno;

        DLOG("Semihosting: return %d\n", *ret);
        break;
    }
    case SEMIHOST_SYS_WRITE:
    {
        uint32_t args[3];
        uint32_t buffer_address;
        int32_t fd;
        uint32_t buffer_len;
        void    *buffer;

        if (mem_read(sl, r1, args, sizeof(args)) != 0) {
            DLOG("Semihosting SYS_WRITE error: cannot read args from target memory\n");
            *ret = -1;
            return (-1);
        }

        fd             = (int32_t)args[0];
        buffer_address = args[1];
        buffer_len     = args[2];

        if (buffer_len > MAX_BUFFER_SIZE) {
            DLOG("Semihosting SYS_WRITE error: buffer size is too big %d\n",
                 buffer_len);
            *ret = buffer_len;
            return (-1);
        }

        buffer = malloc(buffer_len);

        if (buffer == NULL) {
            DLOG("Semihosting SYS_WRITE error: cannot allocate buffer\n");
            *ret = buffer_len;
            return (-1);
        }

        if (mem_read(sl, buffer_address, buffer, buffer_len) != 0) {
            DLOG("Semihosting SYS_WRITE error: cannot read buffer from target memory\n");
            free(buffer);
            *ret = buffer_len;
            return (-1);
        }

        DLOG("Semihosting: write(%d, target_addr:0x%08x, %u)\n", fd, buffer_address, buffer_len);

        *ret = (uint32_t)write(fd, buffer, buffer_len);
        saved_errno = errno;

        if (*ret == (uint32_t)-1) {
            *ret = buffer_len;
        } else {
            *ret -= buffer_len;
        }

        DLOG("Semihosting: return %d\n", *ret);
        free(buffer);
        break;
    }
    case SEMIHOST_SYS_READ:
    {
        uint32_t args[3];
        uint32_t buffer_address;
        int32_t fd;
        uint32_t buffer_len;
        void    *buffer;
        ssize_t read_result;

        if (mem_read(sl, r1, args, sizeof(args)) != 0) {
            DLOG("Semihosting SYS_READ error: cannot read args from target memory\n");
            *ret = -1;
            return (-1);
        }

        fd             = (int32_t)args[0];
        buffer_address = args[1];
        buffer_len     = args[2];

        if (buffer_len > MAX_BUFFER_SIZE) {
            DLOG("Semihosting SYS_READ error: buffer size is too big %d\n", buffer_len);
            *ret = buffer_len;
            return (-1);
        }

        buffer = malloc(buffer_len);

        if (buffer == NULL) {
            DLOG("Semihosting SYS_READ error: cannot allocatebuffer\n");
            *ret = buffer_len;
            return (-1);
        }

        DLOG("Semihosting: read(%d, target_addr:0x%08x, %u)\n", fd, buffer_address,
             buffer_len);

        read_result = read(fd, buffer, buffer_len);
        saved_errno = errno;

        if (read_result == -1) {
            *ret = buffer_len;
        } else {
            if (mem_write(sl, buffer_address, buffer, read_result) != 0) {
                DLOG("Semihosting SYS_READ error: cannot write buffer to target memory\n");
                free(buffer);
                *ret = buffer_len;
                return (-1);
            } else {
                *ret = buffer_len - (uint32_t)read_result;
            }
        }

        DLOG("Semihosting: return %d\n", *ret);
        free(buffer);
        break;
    }
    case SEMIHOST_SYS_ERRNO:
    {
        *ret = (uint32_t)saved_errno;
        DLOG("Semihosting: Errno return %d\n", *ret);
        break;
    }
    case SEMIHOST_SYS_REMOVE:
    {
        uint32_t args[2];
        uint32_t name_address;
        uint32_t name_len;
        char     *name;

        if (mem_read(sl, r1, args, sizeof(args)) != 0) {
            DLOG("Semihosting SYS_REMOVE error: cannot read args from target memory\n");
            *ret = -1;
            return (-1);
        }

        name_address = args[0];
        name_len     = args[1];

        /* Add the trailing zero that is not counted in the length argument (see
         * ARM semihosting specification)
         */
        name_len += 1;

        if (name_len > MAX_BUFFER_SIZE) {
            DLOG("Semihosting SYS_REMOVE error: name buffer size is too big %d\n",
                 name_len);
            *ret = -1;
            return (-1);
        }

        name = malloc(name_len);

        if (name == NULL) {
            DLOG("Semihosting SYS_REMOVE error: cannot allocate name buffer\n");
            *ret = -1;
            return (-1);
        }

        if (mem_read(sl, name_address, name, name_len) != 0) {
            free(name);
            *ret = -1;
            DLOG("Semihosting SYS_REMOVE error: cannot read name from target memory\n");
            return (-1);
        }

        DLOG("Semihosting: unlink('%s')\n", name);
        *ret = (uint32_t)unlink(name);
        saved_errno = errno;
        DLOG("Semihosting: return %d\n", *ret);
        free(name);
        break;
    }
    case SEMIHOST_SYS_SEEK:
    {
        uint32_t args[2];
        int32_t fd;
        off_t offset;

        if (mem_read(sl, r1, args, sizeof(args)) != 0) {
            DLOG("Semihosting SYS_SEEK error: cannot read args from target memory\n");
            *ret = -1;
            return (-1);
        }

        fd = (int32_t)args[0];
        offset = (off_t)args[1];

        DLOG("Semihosting: lseek(%d, %d, SEEK_SET)\n", fd, (int32_t)offset);
        *ret = (uint32_t)lseek(fd, offset, SEEK_SET);
        saved_errno = errno;

        if (*ret != (uint32_t)-1) { *ret = 0; /* Success */ }

        DLOG("Semihosting: return %d\n", *ret);
        break;
    }
    case SEMIHOST_SYS_WRITEC:
    {
        uint8_t c;

        if (mem_read_u8(sl, r1, &c) == 0) {
            fprintf(stderr, "%c", c);
        } else {
            DLOG("Semihosting WRITEC: cannot read target memory at 0x%08x\n", r1);
        }

        break;
    }
    case SEMIHOST_SYS_READC:
    {
        uint8_t c = getchar();
        *ret = c;
        break;
    }
    case SEMIHOST_SYS_WRITE0:
    {
        uint8_t buf[WRITE0_BUFFER_SIZE];

        while (true) {
            if (mem_read(sl, r1, buf, WRITE0_BUFFER_SIZE) != 0) {
                DLOG("Semihosting WRITE0: cannot read target memory at 0x%08x\n", r1);
                return (-1);
            }

            for (int32_t i = 0; i < WRITE0_BUFFER_SIZE; i++) {
                if (buf[i] == 0) { return (0); }

                fprintf(stderr, "%c", buf[i]);
            }

            r1 += WRITE0_BUFFER_SIZE;
        }

        break;
    }
    default:
        fprintf(stderr, "semihosting: unsupported call %#x\n", r0);
        return (-1);
    }
    return (0);
}