summaryrefslogtreecommitdiffstats
path: root/redir.h
blob: 8c54158dba6c89399c65aca0b3a7dfda92aaf59a (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
#include "RedirectionConstants.h"
#include <stdint.h>

enum redir_state {
    REDIR_NONE      =  0,
    REDIR_CONNECT   =  1,
    REDIR_INIT      =  2,
    REDIR_AUTH      =  3,
    REDIR_INIT_SOL  = 10,
    REDIR_RUN_SOL   = 11,
    REDIR_INIT_IDER = 20,
    REDIR_CFG_IDER  = 21,
    REDIR_RUN_IDER  = 22,
    REDIR_CLOSING   = 30,
    REDIR_CLOSED    = 31,
    REDIR_ERROR     = 40,
};

struct redir {
    /* host connection */
    unsigned char     host[64];
    unsigned char     port[16];
    unsigned char     user[64];
    unsigned char     pass[64];

    /* serial-over-lan */
    unsigned char     type[4];
    int               verbose;
    int               trace;
    int               legacy;
    enum redir_state  state;
    unsigned char     err[128]; // state == REDIR_ERROR

    /* ide-redirection */
    unsigned char     filename[256];
    void              *mmap_buf;
    ssize_t           mmap_size;
    unsigned int      tx_bufsize;
    unsigned int      rx_bufsize;
    unsigned int      enable_options;
    unsigned int      tx_length;
    unsigned int      tx_offset;

    int               sock;
    unsigned char     buf[64];
    unsigned int      blen;
    unsigned int      seqno;

    void              *cacert;
    void              *ctx;

    /* callbacks */
    void *cb_data;
    void (*cb_state)(void *cb_data, enum redir_state old, enum redir_state new);
    int (*cb_recv)(void *cb_data, unsigned char *buf, int len);
};

struct __attribute__ ((__packed__)) controls_from_host_message {
    unsigned char type; // 0x29
    unsigned char reserved[3];
    uint32_t      host_sequence_number;
    unsigned char control; 
    unsigned char status;
};

struct __attribute__ ((__packed__)) ider_command_written_message {
    unsigned char type;
    unsigned char reserved[3];
    uint32_t      sequence_number;
    unsigned char cable_sel;          /* 8 */
    unsigned char feature;            /* 9 */
    unsigned char sector_count;       /* 10 */
    unsigned char sector_number;      /* 11 */
    uint16_t      byte_count;         /* 12 */
    unsigned char drive_select;       /* 14 */
    unsigned char command;            /* 15 */
    unsigned char packet_data[12];    /* 16 */
};

struct __attribute__ ((__packed__)) ider_data_regs {
    unsigned char mask;
    unsigned char error;
    unsigned char sector_count;
    unsigned char sector_num;
    unsigned char byte_count_lsb;
    unsigned char byte_count_msb;
    unsigned char drive_select;
    unsigned char status;
};

struct __attribute__ ((__packed__)) ider_command_response_message {
    unsigned char type;
    unsigned char reserved[2];
    unsigned char attributes;
    uint32_t      sequence_number;
    unsigned char cable_sel;         /* unused */
    uint16_t      transfer_bytes;
    unsigned char packet_num;        /* unused */
    unsigned char input_regs[8];     /* unused */
    struct ider_data_regs output;
    unsigned char sense;
    unsigned char asc;
    unsigned char asq;
};

struct __attribute__ ((__packed__)) ider_data_to_host_message {
    unsigned char type;
    unsigned char reserved[2];
    unsigned char attributes;
    uint32_t      sequence_number;
    unsigned char cable_sel;         /* unused */
    uint16_t      transfer_bytes;
    unsigned char packet_num;        /* unused */
    struct ider_data_regs input;
    struct ider_data_regs output;
    unsigned char sense;
    unsigned char asc;
    unsigned char asq;
    unsigned char rsvd2[3];
};

const char *redir_state_name(enum redir_state state);
const char *redir_state_desc(enum redir_state state);

int redir_connect(struct redir *r);
int redir_start(struct redir *r);
int redir_stop(struct redir *r);
int redir_auth(struct redir *r);
int redir_sol_start(struct redir *r);
int redir_sol_stop(struct redir *r);
int redir_sol_send(struct redir *r, unsigned char *buf, int blen);
int redir_sol_recv(struct redir *r);
int redir_ider_start(struct redir *r);
int redir_ider_config(struct redir *r);
int redir_ider_reset(struct redir *r, unsigned int seqno);
int redir_ider_stop(struct redir *r);
int redir_ider_send(struct redir *r, unsigned char *buf, int blen);
int redir_ider_recv(struct redir *r);
int redir_data(struct redir *r);
ssize_t redir_write(struct redir *r, const char *buf, size_t count);

int ider_handle_command(struct redir *r, unsigned int seqno,
			unsigned char device, unsigned char *cdb);