|
1 | 1 | #include <stdbool.h> |
2 | 2 | #include <stdint.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
3 | 5 | #include "fujinet-fuji.h" |
| 6 | +#include "fujinet-fuji-cbm.h" |
| 7 | + |
| 8 | +extern uint16_t ak_creator_id; |
| 9 | +extern uint8_t ak_app_id; |
| 10 | +extern enum AppKeySize ak_appkey_size; |
4 | 11 |
|
5 | 12 | bool fuji_write_appkey(uint8_t key_id, uint16_t count, uint8_t *data) |
6 | 13 | { |
7 | | - return true; |
| 14 | + uint8_t err = 0; |
| 15 | + uint8_t mode = 0; |
| 16 | + uint8_t pl[7]; |
| 17 | + uint8_t *out_data; |
| 18 | + uint16_t data_size = count + 1; // cmd (1 byte) + data length |
| 19 | + |
| 20 | + if (ak_creator_id == 0) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + pl[0] = 0xDC; |
| 25 | + pl[1] = ak_creator_id & 0xFF; |
| 26 | + pl[2] = ak_creator_id >> 8; |
| 27 | + pl[3] = ak_app_id; |
| 28 | + pl[4] = key_id; |
| 29 | + pl[5] = 0x01; // WRITE mode |
| 30 | + pl[6] = 0; // reserved |
| 31 | + |
| 32 | + // send the creator / app / mode values |
| 33 | + if (fuji_cbm_open(FUJI_CMD_CHANNEL, FUJI_CBM_DEV, FUJI_CMD_CHANNEL, sizeof(pl), (uint8_t *) pl) != 0) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + cbm_close(FUJI_CMD_CHANNEL); |
| 37 | + |
| 38 | + out_data = malloc(data_size); |
| 39 | + memset(out_data, 0, data_size); |
| 40 | + |
| 41 | + // now do a write of the key data, on IEC we don't need to send the count, as the write doesn't have to be a fixed size |
| 42 | + out_data[0] = 0xDE; |
| 43 | + memcpy(&out_data[1], data, count); |
| 44 | + |
| 45 | + err = fuji_cbm_open(FUJI_CMD_CHANNEL, FUJI_CBM_DEV, FUJI_CMD_CHANNEL, data_size, out_data); |
| 46 | + free(out_data); |
| 47 | + if (err == 0) { |
| 48 | + cbm_close(FUJI_CMD_CHANNEL); |
| 49 | + return true; |
| 50 | + } |
| 51 | + return false; |
| 52 | + |
8 | 53 | } |
0 commit comments