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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use core::mem;

use crate::guid::Guid;
use super::{FormId, QuestionId, StringId, VarStoreId};

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum IfrTypeKind {
    U8 = 0x00,
    U16 = 0x01,
    U32 = 0x02,
    U64 = 0x03,
    Bool = 0x04,
    Time = 0x05,
    Date = 0x06,
    String = 0x07,
    Other = 0x08,
    Undefined = 0x09,
    Action = 0x0A,
    Buffer = 0x0B,
    Ref = 0x0C,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct HiiTime {
    pub Hour: u8,
    pub Minute: u8,
    pub Second: u8,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct HiiDate {
    pub Year: u16,
    pub Month: u8,
    pub Day: u8,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(packed)] // Fails to have correct size with repr(C)
pub struct HiiRef {
    pub QuestionId: QuestionId,
    pub FormId: FormId,
    pub FormSetGuid: Guid,
    pub DevicePath: StringId,
}

#[derive(Clone, Copy)]
#[repr(packed)] // Fails to have correct size with repr(C)
pub union IfrTypeValue {
    u8: u8,
    u16: u16,
    u32: u32,
    u64: u64,
    b: bool,
    time: HiiTime,
    date: HiiDate,
    string: StringId,
    reference: HiiRef,
    // buffer: [u8]
}

impl IfrTypeValue {
    pub unsafe fn to_enum(self, kind: IfrTypeKind) -> IfrTypeValueEnum {
        match kind {
            IfrTypeKind::U8 => IfrTypeValueEnum::U8(self.u8),
            IfrTypeKind::U16 => IfrTypeValueEnum::U16(self.u16),
            IfrTypeKind::U32 => IfrTypeValueEnum::U32(self.u32),
            IfrTypeKind::U64 => IfrTypeValueEnum::U64(self.u64),
            IfrTypeKind::Bool => IfrTypeValueEnum::Bool(self.b),
            IfrTypeKind::Time => IfrTypeValueEnum::Time(self.time),
            IfrTypeKind::Date => IfrTypeValueEnum::Date(self.date),
            IfrTypeKind::String => IfrTypeValueEnum::String(self.string),
            IfrTypeKind::Ref => IfrTypeValueEnum::Ref(self.reference),
            _ => IfrTypeValueEnum::Other(
                kind,
                mem::transmute(self)
            ),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum IfrTypeValueEnum {
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    Bool(bool),
    Time(HiiTime),
    Date(HiiDate),
    String(StringId),
    Ref(HiiRef),
    Other(IfrTypeKind, [u8; 22])
}

impl IfrTypeValueEnum {
    pub unsafe fn to_union(self) -> (IfrTypeKind, IfrTypeValue) {
        match self {
            IfrTypeValueEnum::U8(u8) => (IfrTypeKind::U8, IfrTypeValue { u8 }),
            IfrTypeValueEnum::U16(u16) => (IfrTypeKind::U16, IfrTypeValue { u16 }),
            IfrTypeValueEnum::U32(u32) => (IfrTypeKind::U32, IfrTypeValue { u32 }),
            IfrTypeValueEnum::U64(u64) => (IfrTypeKind::U64, IfrTypeValue { u64 }),
            IfrTypeValueEnum::Bool(b) => (IfrTypeKind::Bool, IfrTypeValue { b }),
            IfrTypeValueEnum::Time(time) => (IfrTypeKind::Time, IfrTypeValue { time }),
            IfrTypeValueEnum::Date(date) => (IfrTypeKind::Date, IfrTypeValue { date }),
            IfrTypeValueEnum::String(string) => (IfrTypeKind::String, IfrTypeValue { string }),
            IfrTypeValueEnum::Ref(reference) => (IfrTypeKind::Ref, IfrTypeValue { reference }),
            IfrTypeValueEnum::Other(kind, data) => (kind, mem::transmute(data)),
        }
    }
}

#[repr(C)]
pub struct HiiValue {
    pub Kind: IfrTypeKind,
    pub Buffer: *mut u8,
    pub BufferLen: u16,
    pub Value: IfrTypeValue,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct IfrStatementHeader {
    pub Prompt: StringId,
    pub Help: StringId,
}

pub const IFR_FLAG_READ_ONLY: u8 = 0x01;
pub const IFR_FLAG_CALLBACK: u8 = 0x04;
pub const IFR_FLAG_RESET_REQUIRED: u8 = 0x10;
pub const IFR_FLAG_RECONNECT_REQUIRED: u8 = 0x40;
pub const IFR_FLAG_OPTIONS_ONLY: u8 = 0x80;

#[derive(Debug)]
#[repr(packed)] // Has incorrect size if not packed
pub struct IfrQuestionHeader {
    pub Header: IfrStatementHeader,
    pub QuestionId: QuestionId,
    pub VarStoreId: VarStoreId,
    // union { VarName: StringId, VarOffset: u16 }
    pub VarStoreInfo: u16,
    pub Flags: u8,
}

#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum IfrOpCode {
    Form = 0x01,
    Subtitle = 0x02,
    Text = 0x03,
    Image = 0x04,
    OneOf = 0x05,
    Checkbox = 0x06,
    Numeric = 0x07,
    Password = 0x08,
    OneOfOption = 0x09,
    SuppressIf = 0x0A,
    Locked = 0x0B,
    Action = 0x0C,
    ResetButton = 0x0D,
    FormSet = 0x0E,
    Ref = 0x0F,
    NoSubmitIf = 0x10,
    InconsistentIf = 0x11,
    EqIdVal = 0x12,
    EqIdId = 0x13,
    EqIdValList = 0x14,
    And = 0x15,
    Or = 0x16,
    Not = 0x17,
    Rule = 0x18,
    GrayOutIf = 0x19,
    Date = 0x1A,
    Time = 0x1B,
    String = 0x1C,
    Refresh = 0x1D,
    DisableIf = 0x1E,
    Animation = 0x1F,
    ToLower = 0x20,
    ToUpper = 0x21,
    Map = 0x22,
    OrderedList = 0x23,
    VarStore = 0x24,
    VarStoreNameValue = 0x25,
    VarStoreEfi = 0x26,
    VarStoreDevice = 0x27,
    Version = 0x28,
    End = 0x29,
    Match = 0x2A,
    Get = 0x2B,
    Set = 0x2C,
    Read = 0x2D,
    Write = 0x2E,
    Equal = 0x2F,
    NotEqual = 0x30,
    GreaterThan = 0x31,
    GreaterEqual = 0x32,
    LessThan = 0x33,
    LessEqual = 0x34,
    BitwiseAnd = 0x35,
    BitwiseOr = 0x36,
    BitwiseNot = 0x37,
    ShiftLeft = 0x38,
    ShiftRight = 0x39,
    Add = 0x3A,
    Subtract = 0x3B,
    Multiply = 0x3C,
    Divide = 0x3D,
    Modulo = 0x3E,
    RuleRef = 0x3F,
    QuestionRef1 = 0x40,
    QuestionRef2 = 0x41,
    Uint8 =0x42,
    Uint16 = 0x43,
    Uint32 = 0x44,
    Uint64 = 0x45,
    True = 0x46,
    False = 0x47,
    ToUint = 0x48,
    ToString = 0x49,
    ToBoolean = 0x4A,
    Mid = 0x4B,
    Find = 0x4C,
    Token = 0x4D,
    StringRef1 = 0x4E,
    StringRef2 = 0x4F,
    Conditional = 0x50,
    QuestionRef3 = 0x51,
    Zero = 0x52,
    One = 0x53,
    Ones = 0x54,
    Undefined = 0x55,
    Length = 0x56,
    Dup = 0x57,
    This = 0x58,
    Span = 0x59,
    Value = 0x5A,
    Default = 0x5B,
    DefaultStore = 0x5C,
    FormMap = 0x5D,
    Catenate = 0x5E,
    Guid = 0x5F,
    Security = 0x60,
    ModelTag = 0x61,
    RefreshId = 0x62,
    WarningIf = 0x63,
    Match2 = 0x64,
}

#[derive(Debug)]
#[repr(C)]
pub struct IfrOpHeader {
    pub OpCode: IfrOpCode,
    pub Length_Scope: u8,
}

impl IfrOpHeader {
    pub fn Length(&self) -> u8 {
        self.Length_Scope & 0x7F
    }

    pub fn Scope(&self) -> bool {
        self.Length_Scope & 0x80 == 0x80
    }

    pub unsafe fn cast<T>(&self) -> Option<&T> {
        if self.Length() as usize >= mem::size_of::<T>() {
            Some(&*(self as *const Self as *const T))
        } else {
            None
        }
    }
}

macro_rules! unsafe_field {
    ($struct: ident, $field:ident, $type:ty) => (
        pub fn $field(&self) -> Option<&$type> {
            unsafe {
                let self_ptr = self as *const Self;
                let unsafe_self = &*(self_ptr as *const $struct);
                let field_ptr = &unsafe_self.$field as *const $type;
                let length = field_ptr.add(1) as usize - self_ptr as usize;
                if self.Header.Length() as usize >= length {
                    Some(&*field_ptr)
                } else {
                    None
                }
            }
        }
    );
}

#[derive(Debug)]
#[repr(C)]
pub struct IfrAction {
    pub Header: IfrOpHeader,
    pub QuestionHeader: IfrQuestionHeader,
}

#[repr(C)]
struct UnsafeIfrAction {
    safe: IfrAction,
    QuestionConfig: StringId,
}

impl IfrAction {
    unsafe_field!(UnsafeIfrAction, QuestionConfig, StringId);
}

#[derive(Debug)]
#[repr(packed)] // Has incorrect size if not packed
pub struct IfrCheckbox {
    pub Header: IfrOpHeader,
    pub Question: IfrQuestionHeader,
    pub Flags: u8,
}

#[derive(Debug)]
#[repr(C)]
pub struct IfrForm {
    pub Header: IfrOpHeader,
    pub FormId: FormId,
    pub FormTitle: StringId,
}

#[derive(Debug)]
#[repr(packed)] // Has incorrect size if not packed
pub struct IfrNumeric {
    pub Header: IfrOpHeader,
    pub Question: IfrQuestionHeader,
    pub Flags: u8,
    //TODO: MinValue, MaxValue, Step
}

#[derive(Debug)]
#[repr(C)]
pub struct IfrOneOf {
    pub Header: IfrOpHeader,
    pub Question: IfrQuestionHeader,
    pub Flags: u8,
    //TODO: MinValue, MaxValue, Step
}

#[repr(C)]
pub struct IfrOneOfOption {
    pub Header: IfrOpHeader,
    pub Option: StringId,
    pub Flags: u8,
    pub Kind: IfrTypeKind,
    pub Value: IfrTypeValue,
}

#[derive(Debug)]
#[repr(C)]
pub struct IfrOrderedList {
    pub Header: IfrOpHeader,
    pub Question: IfrQuestionHeader,
    pub MaxContainers: u8,
    pub Flags: u8,
}

#[derive(Debug)]
#[repr(C)]
pub struct IfrRef {
    pub Header: IfrOpHeader,
    pub Question: IfrQuestionHeader
}


#[repr(C)]
struct UnsafeIfrRef {
    safe: IfrRef,
    FormId: FormId,
    QuestionId: QuestionId,
    FormSetId: Guid,
    DevicePath: *const u16,
}

impl IfrRef {
    unsafe_field!(UnsafeIfrRef, FormId, FormId);
    unsafe_field!(UnsafeIfrRef, QuestionId, QuestionId);
    unsafe_field!(UnsafeIfrRef, FormSetId, Guid);
    unsafe_field!(UnsafeIfrRef, DevicePath, *const u16);
}

#[repr(packed)] // Has incorrect size if not packed
pub struct IfrSubtitle {
    pub Header: IfrOpHeader,
    pub Statement: IfrStatementHeader,
    pub Flags: u8,
}