Root/fw/image.c

Source at commit 87b1fbb66c48eb705d74fd3c612423d4bd4b86dc created 11 years 1 day ago.
By Werner Almesberger, tornado/cpu/: new version of the CPU board (WIP)
1/*
2 * fw/image.c - Image data (upload and conversion)
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdbool.h>
15#include <stdint.h>
16#include <string.h>
17
18#include "io.h"
19#include "hash.h"
20#include "proto.h"
21#include "dispatch.h"
22#include "secret.h"
23#include "image.h"
24
25
26static struct line images[2][MAX_LINES];
27static const struct line *next_image;
28static struct line *p, *end;
29static bool failed;
30
31const struct line *image = images[0];
32
33
34#define MAP(port, value, group) ( \
35    ((value) & 1 ? MASK(port, LED_##group##1) : 0) | \
36    ((value) & 2 ? MASK(port, LED_##group##2) : 0) | \
37    ((value) & 4 ? MASK(port, LED_##group##3) : 0) | \
38    ((value) & 8 ? MASK(port, LED_##group##4) : 0) | \
39    ((value) & 16 ? MASK(port, LED_##group##5) : 0) | \
40    ((value) & 32 ? MASK(port, LED_##group##6) : 0) | \
41    ((value) & 64 ? MASK(port, LED_##group##7) : 0) | \
42    ((value) & 128 ? MASK(port, LED_##group##8) : 0))
43
44
45struct line localize_line(uint8_t p0, uint8_t p1)
46{
47    struct line res;
48
49    res.cb = MAP(B, p0, A) | MAP(B, p1, B) |
50        MAP(C, p0, A) | MAP(C, p1, B);
51    res.d = MAP(D, p0, A) | MAP(D, p1, B);
52    return res;
53}
54
55
56static void add_payload(const uint8_t *payload)
57{
58    uint8_t i;
59
60    for (i = 0; i != PAYLOAD && p != end; i += 2)
61        *p++ = localize_line(payload[i], payload[i+1]);
62}
63
64
65static bool image_more(uint8_t seq, uint8_t limit, const uint8_t *payload)
66{
67    switch (limit-seq) {
68    default:
69        add_payload(payload);
70        /* fall through */
71    case 3:
72    case 2:
73        hash_merge(payload, PAYLOAD);
74        break;
75    case 1:
76        hash_end();
77        failed = !hash_eq(payload, PAYLOAD, 0);
78        break;
79    case 0:
80        if (!hash_eq(payload, PAYLOAD, PAYLOAD))
81            failed = 1;
82        if (failed)
83            return 0;
84        image = next_image;
85        break;
86    }
87    return 1;
88}
89
90
91static bool image_first(uint8_t limit, const uint8_t *payload)
92{
93    hash_init();
94    hash_merge_progmem(image_secret, sizeof(image_secret));
95    if (image == images[0])
96        p = images[1];
97    else
98        p = images[0];
99    next_image = p;
100    end = p+MAX_LINES;
101    memset(p, 0, (char *) end-(char *) p);
102    failed = 0;
103    return image_more(0, limit, payload);;
104}
105
106
107struct handler image_handler = {
108        .type = IMAGE,
109        .first = image_first,
110        .more = image_more,
111};
112

Archive Download this file

Branches:
master
tornado-v1



interactive