summaryrefslogtreecommitdiff
path: root/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/FfsHeader.java
blob: 4af491950da579885d31f119979b08e7d741bf99 (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
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
/** @file
 FfsHeader 

 FfsHeader class describe the struct of Ffs file header.
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

 **/
package org.tianocore.framework.tasks;

import org.apache.tools.ant.BuildException;

/**
  FfsHeader
  
  FfsHeader class describe the struct of Ffs file header.
**/
public class FfsHeader {

    /**
      FfsGuid
      
      FfsGuid is interal class of FfsHeader, it describe the struct of Guid.
    **/
    public class FfsGuid {

        int    data1      = 0;
        short  data2      = 0;
        short  data3      = 0;
        byte[] data4      = new byte[8];        
        byte[] dataBuffer = new byte[16];

        /**
          bufferToStruct 
          
          This function is to convert GUID to ffsGuid class member.
          
          @param   dataBuffer   Buffer contained the GUID value in byte.
                                For example: if the input string as : "A6F691AC
                                31C8 4444 854C E2C1A6950F92"
                                Then Data1: AC91F6A6
                                Data2: C831
                                Data3: 4444
                                Data4: 4C85E2C1A6950F92
        **/     
        public void bufferToStruct (byte[] dataBuffer){
            if (dataBuffer.length != 16) {
                throw new BuildException ("Buffer is not fitting GUID type!");
            }

            data1 = (int)(dataBuffer[3]& 0xff);
            data1 = data1 << 8;     
            data1 = (int)data1 | (dataBuffer[2]& 0xff);         
            data1 = ((data1 << 8) & 0xffff00)   | (dataBuffer[1]& 0xff);
            data1 = ((data1 << 8) & 0xffffff00) | (dataBuffer[0]& 0xff);


            data2 = (short) (dataBuffer[5] & 0xff);
            data2 = (short)((data2 << 8) | (dataBuffer[4]& 0xff));

            data3 = (short)(dataBuffer[7] & 0xff);
            data3 = (short)((data3 << 8) | (dataBuffer[6] & 0xff));

            for (int i = 0; i < 8; i++) {
                data4[i] = dataBuffer[i+8];
            }

        }

        /**
          structToBuffer
          
          This function is to store ffsHeader class member to buffer.
          
          @return        Byte buffer which contained the ffsHeader class member
        **/
        public byte[] structToBuffer (){

            byte[] buffer = new byte [16];  

            buffer[3] = (byte)(data1 & 0x000000ff);
            buffer[2] = (byte)((data1 & 0x0000ff00)>> 8);
            buffer[1] = (byte)((data1 & 0x00ff0000)>> 16); 
            buffer[0] = (byte)((data1 & 0xff000000)>> 24);

            buffer[5] = (byte)(data2 & 0x00ff);
            buffer[4] = (byte)((data2 & 0xff00)>> 8);

            buffer[7] = (byte)(data3 & 0x00ff);
            buffer[6] = (byte)((data3 & 0xff00)>> 8);

            for (int i = 8; i < 16; i++) {
                buffer[i] = data4[i-8];
            }               
            return buffer;
        }


    }

    /**
      integrityCheckSum
      
      This class is used to record the struct of checksum.
    **/
    public class IntegrityCheckSum {
        byte header;
        byte file;
    }

    ///
    /// Guid
    ///
    FfsGuid name = new FfsGuid();

    ///
    /// CheckSum
    ///
    IntegrityCheckSum integrityCheck = new IntegrityCheckSum();

    ///
    /// File type
    ///
    byte   fileType;
    ///
    /// Ffs attributes.
    ///
    byte   ffsAttributes;
    ///
    /// Ffs file size
    ///
    byte[] ffsFileSize = new byte[3];
    ///
    /// Ffs state.
    ///
    byte   ffsState;

    /**
      structToBuffer
      
      This function is to store FfsHeader class member to buffer.
      
      @return   Byte buffer which contained the FfsHeader class member.
    **/
    public byte[] structToBuffer () {
        int i;
        byte[] buffer1;         
        byte[] buffer = new byte[24];
        buffer1       = name.structToBuffer();

        for (i = 0; i < 16; i++) {
            buffer[i] = buffer1[i];
        }

        buffer[16] = integrityCheck.header;
        buffer[17] = integrityCheck.file;
        buffer[18] = fileType;
        buffer[19] = ffsAttributes;

        for (i=20; i < 23; i++) {
            buffer[i] = ffsFileSize[i-20];
        }

        buffer[23] = ffsState;
        return buffer;
    }

    /**
      getSize
      
      This function is to get the size of FfsHeader in byte.
      
      @return          The size of FfsHeader.
    **/
    public int getSize(){
        return 24;
    }
}