aboutsummaryrefslogtreecommitdiff
path: root/tests/test-mkmif.c
blob: ab5801ea181d08037e64fd0552172fa38a8a58d9 (plain) (blame)
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
/*
 * Test Joachim's MKMIF core.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include <sys/time.h>

#include <hal.h>

#define SCLK_DIV 0x00000020

typedef union {
    uint8_t byte[4];
    uint32_t word;
} byteword_t;

static hal_error_t sclk_test(hal_core_t *core, const uint32_t divisor)
{
    uint32_t readback;
    hal_error_t err;

    printf("Trying to adjust the clockspeed.\n");

    if ((err = hal_mkmif_set_clockspeed(core, divisor)) != HAL_OK) {
        printf("hal_mkmif_set_clockspeed: %s\n", hal_error_string(err));
        return err;
    }
    if ((err = hal_mkmif_get_clockspeed(core, &readback)) != HAL_OK) {
        printf("hal_mkmif_get_clockspeed: %s\n", hal_error_string(err));
        return err;
    }
    if (readback != divisor) {
        printf("expected %x, got %x\n", (unsigned int)divisor, (unsigned int)readback);
        return HAL_ERROR_IO_UNEXPECTED;
    }
    return HAL_OK;
}

static hal_error_t init_test(hal_core_t *core)
{
    hal_error_t err;

    printf("Trying to init to the memory in continuous mode.\n");

    if ((err = hal_mkmif_init(core)) != HAL_OK) {
        printf("hal_mkmif_init: %s\n", hal_error_string(err));
        return err;
    }

    return HAL_OK;
}

static hal_error_t write_test(hal_core_t *core)
{
    uint32_t write_data;
    uint32_t write_address;
    int i;
    hal_error_t err;
    
    for (write_data = 0x01020304, write_address = 0, i = 0;
         i < 0x10;
         write_data += 0x01010101, write_address += 4, ++i) {

        printf("Trying to write 0x%08x to memory address 0x%08x.\n", 
               (unsigned int)write_data, (unsigned int)write_address);

        if ((err = hal_mkmif_write_word(core, write_address, write_data)) != HAL_OK) {
            printf("hal_mkmif_write: %s\n", hal_error_string(err));
            return err;
        }
    }

    return HAL_OK;
}

static hal_error_t read_test(hal_core_t *core)
{
    uint32_t read_data;
    uint32_t read_address;
    int i;
    hal_error_t err;
    
    for (read_address = 0, i = 0;
         i < 0x10;
         read_address += 4, ++i) {

        printf("Trying to read from memory address 0x%08x.\n", (unsigned int)read_address);

        if ((err = hal_mkmif_read_word(core, read_address, &read_data)) != HAL_OK) {
            printf("hal_mkmif_read: %s\n", hal_error_string(err));
            return err;
        }
        printf("Data read: 0x%08x\n", (unsigned int)read_data);
    }

    return HAL_OK;
}

static hal_error_t write_read_test(hal_core_t *core)
{
    uint32_t data;
    uint32_t readback;
    hal_error_t err;

    printf("Trying to write 0xdeadbeef to the memory and then read back.\n");

    data = 0xdeadbeef;

    if ((err = hal_mkmif_write_word(core, 0x00000000, data)) != HAL_OK) {
        printf("write error: %s\n", hal_error_string(err));
        return err;
    }
      
    if ((err = hal_mkmif_read_word(core, 0x00000000, &readback)) != HAL_OK) {
        printf("read error: %s\n", hal_error_string(err));
        return err;
    }

    if (readback != data) {
        printf("read %08x, expected %08x\n", (unsigned int)readback, (unsigned int)data);
        return HAL_ERROR_IO_UNEXPECTED;
    }
      
    return HAL_OK;
}

int main(void)
{
    hal_core_t *core = hal_core_find(MKMIF_NAME, NULL);

    if (core == NULL) {
        printf("MKMIF core not present, not testing.\n");
        return HAL_ERROR_CORE_NOT_FOUND;
    }

    hal_io_set_debug(1);

    return
        sclk_test(core, SCLK_DIV) ||
        init_test(core) ||
        write_read_test(core) ||
        write_test(core) ||
        read_test(core);
}
n class="p">(const uint8_t * const bytes, const size_t bytes_len, uint32_t *attribute_type, size_t *attribute_len) { if (bytes == NULL || bytes_len < hal_ks_attribute_header_size || attribute_type == NULL || attribute_len == NULL) return HAL_ERROR_BAD_ARGUMENTS; *attribute_type = ((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | (bytes[3] << 0)); *attribute_len = ((bytes[4] << 8) | (bytes[5] << 0)); return HAL_OK; } static inline hal_error_t write_header(uint8_t *bytes, const size_t bytes_len, const uint32_t attribute_type, const size_t attribute_len) { if (bytes == NULL || bytes_len < hal_ks_attribute_header_size) return HAL_ERROR_BAD_ARGUMENTS; bytes[0] = (attribute_type >> 24) & 0xFF; bytes[1] = (attribute_type >> 16) & 0xFF; bytes[2] = (attribute_type >> 8) & 0xFF; bytes[3] = (attribute_type >> 0) & 0xFF; bytes[4] = (attribute_len >> 8) & 0xFF; bytes[5] = (attribute_len >> 0) & 0xFF; return HAL_OK; } hal_error_t hal_ks_attribute_scan(const uint8_t * const bytes, const size_t bytes_len, hal_pkey_attribute_t *attributes, const unsigned attributes_len, size_t *total_len) { if (bytes == NULL) return HAL_ERROR_BAD_ARGUMENTS; const uint8_t *b = bytes; const uint8_t * const end = bytes + bytes_len; for (int i = 0; i < attributes_len; i++) { uint32_t type; size_t length; hal_error_t err = read_header(b, end - b, &type, &length); if (err != HAL_OK) return err; if (b + hal_ks_attribute_header_size + length > end) return HAL_ERROR_BAD_ATTRIBUTE_LENGTH; b += hal_ks_attribute_header_size; if (attributes != NULL) { attributes[i].type = type; attributes[i].length = length; attributes[i].value = b; } b += length; } if (total_len != NULL) *total_len = b - bytes; return HAL_OK; } hal_error_t hal_ks_attribute_delete(uint8_t *bytes, const size_t bytes_len, hal_pkey_attribute_t *attributes, unsigned *attributes_len, size_t *total_len, const uint32_t type) { if (bytes == NULL || attributes == NULL || attributes_len == NULL || total_len == NULL) return HAL_ERROR_BAD_ARGUMENTS; /* * Search for attribute by type. Note that there can be only one * attribute of any given type. */ int i = 0; while (i < *attributes_len && attributes[i].type != type) i++; /* If not found, great, it's already deleted from the key. */ if (i == *attributes_len) return HAL_OK; const size_t delete_length = hal_ks_attribute_header_size + attributes[i].length; const size_t delete_offset = (uint8_t*) attributes[i].value - hal_ks_attribute_header_size - bytes; if (delete_offset + delete_length > *total_len) return HAL_ERROR_IMPOSSIBLE; memmove(bytes + delete_offset, bytes + delete_offset + delete_length, *total_len - delete_length - delete_offset); return hal_ks_attribute_scan(bytes, bytes_len, attributes, --*attributes_len, total_len); } hal_error_t hal_ks_attribute_insert(uint8_t *bytes, const size_t bytes_len, hal_pkey_attribute_t *attributes, unsigned *attributes_len, size_t *total_len, const uint32_t type, const uint8_t * const value, const size_t value_len) { if (bytes == NULL || attributes == NULL || attributes_len == NULL || total_len == NULL || value == NULL) return HAL_ERROR_BAD_ARGUMENTS; /* Delete the existing attribute value (if present), then write the new value. */ hal_error_t err = hal_ks_attribute_delete(bytes, bytes_len, attributes, attributes_len, total_len, type); if (err != HAL_OK) return err; if (*total_len + hal_ks_attribute_header_size + value_len > bytes_len) return HAL_ERROR_RESULT_TOO_LONG; uint8_t *b = bytes + *total_len; if ((err = write_header(b, bytes_len - *total_len, type, value_len)) != HAL_OK) return err; memcpy(b + hal_ks_attribute_header_size, value, value_len); return hal_ks_attribute_scan(bytes, bytes_len, attributes, ++*attributes_len, total_len); } /* * Local variables: * indent-tabs-mode: nil * End: */