File: uefifind.cpp

package info (click to toggle)
uefitool 0.28.0%2BA73-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,728 kB
  • sloc: ansic: 55,322; cpp: 23,375; sh: 43; xml: 23; makefile: 5
file content (148 lines) | stat: -rw-r--r-- 4,955 bytes parent folder | download | duplicates (2)
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
/* uefifind.cpp

Copyright (c) 2016, Nikolaj Schlej. 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.

*/

#include "uefifind.h"
#include <fstream>
#include <set>


UEFIFind::UEFIFind()
{
    model = new TreeModel();
    ffsParser = new FfsParser(model);
    initDone = false;
}

UEFIFind::~UEFIFind()
{
    delete ffsParser;
    delete model;
    model = NULL;
}

USTATUS UEFIFind::init(const UString & path)
{
    UByteArray buffer;
    if (false == readFileIntoBuffer(path, buffer))
        return U_FILE_OPEN;

    USTATUS result = ffsParser->parse(buffer);
    if (result)
        return result;

    initDone = true;
    return U_SUCCESS;
}

USTATUS UEFIFind::findFileRecursive(const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files)
{
    if (!index.isValid())
        return U_SUCCESS;

    if (hexPattern.isEmpty())
        return U_INVALID_PARAMETER;

    const char *hexPatternRaw = hexPattern.toLocal8Bit();
    std::vector<UINT8> pattern, patternMask;
    if (!makePattern(hexPatternRaw, pattern, patternMask))
        return U_INVALID_PARAMETER;

    // Check for "all substrings" pattern
    size_t count = 0;
    for (size_t i = 0; i < patternMask.size(); i++)
        if (patternMask[i] == 0)
            count++;
    if (count == patternMask.size())
        return U_SUCCESS;

    bool hasChildren = (model->rowCount(index) > 0);
    for (int i = 0; i < model->rowCount(index); i++) {
        findFileRecursive(index.model()->index(i, index.column(), index), hexPattern, mode, files);
    }

    // TODO: handle a case where an item has both compressed and uncompressed bodies
    UByteArray data;
    if (hasChildren) {
        if (mode == SEARCH_MODE_HEADER)
            data = model->header(index);
        else if (mode == SEARCH_MODE_ALL)
            data = model->header(index) + model->body(index);
    }
    else {
        if (mode == SEARCH_MODE_HEADER)
            data = model->header(index);
        else if (mode == SEARCH_MODE_BODY)
            data = model->body(index);
        else
            data = model->header(index) + model->body(index);
    }

    const UINT8 *rawData = reinterpret_cast<const UINT8 *>(data.constData());
    INTN offset = findPattern(pattern.data(), patternMask.data(), pattern.size(), rawData, data.size(), 0);

    // For patterns that cross header|body boundary, skip patterns entirely located in body, since
    // children search above has already found them.
    if (hasChildren && mode == SEARCH_MODE_ALL && offset >= model->header(index).size()) {
        offset = -1;
    }

    if (offset >= 0) {
        if (model->type(index) != Types::File) {
            UModelIndex parentFile = model->findParentOfType(index, Types::File);
            if (model->type(index) == Types::Section && model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID)
                files.insert(std::pair<UModelIndex, UModelIndex>(parentFile, index));
            else
                files.insert(std::pair<UModelIndex, UModelIndex>(parentFile, UModelIndex()));
        }
        else {
            files.insert(std::pair<UModelIndex, UModelIndex>(index, UModelIndex()));
        }
    }

    return U_SUCCESS;
}

USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPattern, UString & result)
{
    UModelIndex root = model->index(0, 0);
    std::set<std::pair<UModelIndex, UModelIndex> > files;

    result.clear();

    USTATUS returned = findFileRecursive(root, hexPattern, mode, files);
    if (returned)
        return returned;
    
    if (count) {
        if (!files.empty())
            result += usprintf("%lu\n", files.size());
        return U_SUCCESS;
    }

    for (std::set<std::pair<UModelIndex, UModelIndex> >::const_iterator citer = files.begin(); citer != files.end(); ++citer) {
        UByteArray data(16, '\x00');
        std::pair<UModelIndex, UModelIndex> indexes = *citer;
        if (!model->hasEmptyHeader(indexes.first))
            data = model->header(indexes.first).left(16);
        result += guidToUString(readUnaligned((const EFI_GUID*)data.constData()));

        // Special case of freeform subtype GUID files
        if (indexes.second.isValid() && model->subtype(indexes.second) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) {
            data = model->header(indexes.second);
            result += UString(" ") + (guidToUString(readUnaligned((const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER)))));
        }
        
        result += UString("\n");
    }
    return U_SUCCESS;
}