summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/fru/default-fru/mkfru.cpp
blob: afadbd324043e225c99023e65016bf37922bb0f3 (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
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
/*
// Copyright (c) 2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Abstract:   default FRU generation
//
*/

#include <fstream>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>

constexpr uint8_t fillChar = '.';
constexpr uint8_t eof = 0xc1;
const std::string intel = "Intel Corporation";

// round up to nearest block size (power of 2)
constexpr size_t blockRound(size_t len, size_t blk)
{
    return ((len) + (((blk) - ((len) & ((blk)-1))) & ((blk)-1)));
}

uint8_t mklen(uint8_t len)
{
    return static_cast<uint8_t>((0x3 << 6) | len);
}

struct FruEntry
{
    static constexpr size_t fruBlockSize = 8; // type, length, checksum
    static constexpr size_t fixedBytes = 3;   // type, length, checksum
    FruEntry() = delete;
    FruEntry(const std::vector<uint8_t>& contents)
    {
        constexpr size_t verOffset = 0;
        constexpr size_t lenOffset = 1;
        value.resize(blockRound(fixedBytes + contents.size(), fruBlockSize));
        value[verOffset] = 1;
        value[lenOffset] = blocks();
        std::copy(contents.begin(), contents.end(), value.begin() + 2);
        addChecksum();
    }

    void addChecksum()
    {
        int sum = std::accumulate(value.begin(), value.end(), 0);
        value.back() = static_cast<uint8_t>(256 - sum & 0xff);
    }

    uint8_t blocks() const
    {
        return static_cast<uint8_t>(value.size() / 8);
    }

    std::vector<uint8_t> value;
};

size_t fillDots(std::vector<uint8_t>::iterator start, size_t count)
{
    *start++ = mklen(count); // prefix with (0xc0 | count)
    auto end = start + count++;
    std::fill(start, end, '.');
    return count;
}

size_t fillStr(std::vector<uint8_t>::iterator start, const std::string& str)
{
    size_t count = str.size();
    *start++ = mklen(count++); // prefix with (0xc0 | count)
    std::copy(str.begin(), str.end(), start);
    return count;
}

std::vector<uint8_t> genChassisContents()
{
    constexpr size_t pnSize = 18;
    constexpr size_t snSize = 18;
    constexpr size_t amSize = 31;
    constexpr size_t headerSize = 1;
    constexpr size_t contentSize = headerSize + 1 + pnSize + 1 + snSize + 1 +
                                   amSize + 1 + amSize + sizeof(eof);
    std::vector<uint8_t> data(contentSize);
    size_t offset = 0;
    // chassis type (main server chassis)
    data[offset++] = 0x17;
    // chassis part number
    offset += fillDots(data.begin() + offset, pnSize);
    // chassis serial number
    offset += fillDots(data.begin() + offset, snSize);
    // info am1
    offset += fillDots(data.begin() + offset, amSize);
    // info am2
    offset += fillDots(data.begin() + offset, amSize);
    data[offset] = eof;

    return data;
}

std::vector<uint8_t> genBoardContents(const std::string& name)
{
    constexpr size_t headerSize = 4;
    constexpr size_t snSize = 12;
    constexpr size_t pnSize = 10;
    const std::string version = "FRU Ver 0.01";
    size_t contentSize = headerSize + 1 + name.size() + 1 + intel.size() + 1 +
                         snSize + 1 + pnSize + 1 + version.size() + sizeof(eof);
    std::vector<uint8_t> data(contentSize);
    size_t offset = 0;
    // chassis type (main server chassis)
    data[offset++] = 0; // language code
    data[offset++] = 0; // mfg date/time
    data[offset++] = 0; // mfg date/time
    data[offset++] = 0; // mfg date/time
    // manufacturer name
    offset += fillStr(data.begin() + offset, intel);
    // product name
    offset += fillStr(data.begin() + offset, name);
    // board sn
    offset += fillDots(data.begin() + offset, snSize);
    // board pn
    offset += fillDots(data.begin() + offset, pnSize);
    // fru version string
    offset += fillStr(data.begin() + offset, version);
    data[offset] = eof;

    return data;
}

std::vector<uint8_t> genProductContents(const std::string& name)
{
    constexpr size_t headerSize = 1;
    constexpr size_t pnSize = 10;
    constexpr size_t pvSize = 20;
    constexpr size_t snSize = 12;
    constexpr size_t atSize = 20;
    constexpr size_t idSize = 0;
    const std::string version = "FRU Ver 0.01";
    size_t contentSize = headerSize + 1 + intel.size() + 1 + name.size() + 1 +
                         pnSize + 1 + pvSize + 1 + snSize + 1 + atSize + 1 +
                         idSize + sizeof(eof);
    std::vector<uint8_t> data(contentSize);
    size_t offset = 0;
    // chassis type (main server chassis)
    data[offset++] = 0; // language code
    // manufacturer name
    offset += fillStr(data.begin() + offset, intel);
    // product name
    offset += fillStr(data.begin() + offset, name);
    // product part number
    offset += fillDots(data.begin() + offset, pnSize);
    // product version
    offset += fillDots(data.begin() + offset, pvSize);
    // product serial number
    offset += fillDots(data.begin() + offset, snSize);
    // product asset tag
    offset += fillDots(data.begin() + offset, atSize);
    // empty fru file id
    offset += fillDots(data.begin() + offset, idSize);
    data[offset] = eof;

    return data;
}

int createFru(const std::string& name)
{
    std::vector<uint8_t> internal{1, 0, 0, 0, 0, 0, 0, 1}; // fixed data
    FruEntry chassis(genChassisContents());
    FruEntry board(genBoardContents(name));
    FruEntry product(genProductContents(name));
    uint8_t offset = 1; // room for header's offset
    FruEntry header({
        offset += 1, // internal size
        offset += chassis.blocks(),
        offset += board.blocks(),
    });
    std::string filename = name + ".fru.bin";
    std::ofstream output(filename);
    std::ostream_iterator<uint8_t> outputIter(output);
    std::copy(header.value.begin(), header.value.end(), outputIter);
    std::copy(internal.begin(), internal.end(), outputIter);
    std::copy(chassis.value.begin(), chassis.value.end(), outputIter);
    std::copy(board.value.begin(), board.value.end(), outputIter);
    std::copy(product.value.begin(), product.value.end(), outputIter);
    constexpr size_t minFruSize = 0x1ff;
    size_t fruSize = header.value.size() + internal.size() +
                     chassis.value.size() + board.value.size() +
                     product.value.size();
    if (fruSize < minFruSize)
    {
        std::vector<uint8_t> padding(minFruSize - fruSize);
        std::copy(padding.begin(), padding.end(), outputIter);
    }
    output.close();
    return 0;
}

int main(int argc, const char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " <'Product Name'>\n";
        return 1;
    }
    return createFru(argv[1]);
}