summaryrefslogtreecommitdiffstats
path: root/sw/programmer/linux/src/main.c
blob: 1bfd3599843531b106e7a988ee958e3746ab9114 (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
#include "config.h"
#include "ui.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>

void out(const char *msg)
{
    printf("%s", msg);
}

int main(int argc, char *argv[])
{
    int opt;
    int use_ui = 0;

    unsigned long baudrate;
    char *file, *device, *baud;

    file = device = baud = NULL;

    while ((opt = getopt(argc, argv, "gf:d:b:")) != -1) {
        switch (opt) {
        case 'g':
            use_ui = 1;
            break;
        case 'f':
            file = optarg;
            break;
        case 'd':
            device = optarg;
            break;
        case 'b':
            baud = optarg;
            break;
        }
    }

    if (use_ui) {
        ui_init(&argc, &argv);
        return 0;
    }

    if (file == NULL || device == NULL || baud == NULL) {
        perror("Missing argument\n");
        return -1;
    }

    baudrate = strtoul(baud, NULL, 0);
    if (errno == ERANGE) {
        perror("Invalid baudrate\n");
        return -1;
    }

    if (flash_open(device, baudrate) < 0) {
        perror("Failed to open device\n");
        return -2;
    }

    flash_write(file, out);
    flash_close();

    return 0;
}