summaryrefslogtreecommitdiff
path: root/ArmPkg/Library/BdsLib/BdsFilePath.c
blob: b1460b9c9db1d44e85afd6d378043e19c9d0b49a (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
/** @file
*
*  Copyright (c) 2011, ARM Limited. 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 "BdsInternal.h"

// Count the number of DevicePath Node
static UINTN NumberNodeFromDevicePath(
    IN  EFI_DEVICE_PATH_PROTOCOL* DevicePath
) {
    UINTN NumberDevicePathNode = 0;

    while (!IsDevicePathEnd (DevicePath)) {
        NumberDevicePathNode++;
        DevicePath = NextDevicePathNode(DevicePath);
    }
    return NumberDevicePathNode;
}

// Extract the FilePath from the Device Path
CHAR16* BdsExtractFilePathFromDevicePath(
    IN  CONST CHAR16    *StrDevicePath,
    IN  UINTN           NumberDevicePathNode
) {
    UINTN       Node;
    CHAR16      *Str;

    Str = (CHAR16*)StrDevicePath;
    Node = 0;
    while ((Str != NULL) && (*Str != L'\0') && (Node < NumberDevicePathNode)) {
        if ((*Str == L'/') || (*Str == L'\\')) {
            Node++;
        }
        Str++;
    }

    if (*Str == L'\0') {
        return NULL;
    } else {
        return Str;
    }
}

EFI_STATUS
BdsLoadDevicePath(
    IN  EFI_DEVICE_PATH_PROTOCOL* DevicePath,
    OUT EFI_HANDLE                *Handle
) {
    EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath;
    EFI_STATUS                  Status;

    if ((DevicePath == NULL) || (Handle == NULL)) {
        return EFI_INVALID_PARAMETER;
    }

    do {
        RemainingDevicePath = DevicePath;
        // The LocateDevicePath() function locates all devices on DevicePath that support Protocol and returns
        // the handle to the device that is closest to DevicePath. On output, the device path pointer is modified
        // to point to the remaining part of the device path
        Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid,&RemainingDevicePath,Handle);
        if (!EFI_ERROR (Status)) {
            // Recursive = FALSE: We do not want to start all the device tree
            Status = gBS->ConnectController (*Handle, NULL, RemainingDevicePath, FALSE);
        }

        // We need to check if RemainingDevicePath does not point on the last node. Otherwise, calling
        // NextDevicePathNode() will return an undetermined Device Path Node
        if (!IsDevicePathEnd (RemainingDevicePath)) {
            RemainingDevicePath = NextDevicePathNode (RemainingDevicePath);
        }
    } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));

    if (!EFI_ERROR (Status)) {
        // Now, we have got the whole Device Path connected, call again ConnectController to ensure all the supported Driver
        // Binding Protocol are connected (such as DiskIo and SimpleFileSystem)
        RemainingDevicePath = DevicePath;
        Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid,&RemainingDevicePath,Handle);
        if (!EFI_ERROR (Status)) {
            Status = gBS->ConnectController (*Handle, NULL, RemainingDevicePath, FALSE);
            if (EFI_ERROR (Status)) {
                // If the last node is a Memory Map Device Path just return EFI_SUCCESS.
                if ((RemainingDevicePath->Type == HARDWARE_DEVICE_PATH) && (RemainingDevicePath->SubType == HW_MEMMAP_DP)) {
                    Status = EFI_SUCCESS;
                }
            }
        }
    } else if (IsDevicePathEnd (RemainingDevicePath)) {
        // Case when the DevicePath contains a MemoryMap Device Path Node and all drivers are connected.
        // Ensure the Device Path exists
        RemainingDevicePath = DevicePath;
        Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid,&RemainingDevicePath,Handle);
    }

    return Status;
}


EFI_STATUS
BdsLoadFilePath (
    IN  CONST CHAR16        *DeviceFilePath,
    OUT BDS_FILE            *File
) {
    EFI_DEVICE_PATH_PROTOCOL            *DevicePath;
    EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL  *EfiDevicePathFromTextProtocol;
    EFI_STATUS Status;
    EFI_HANDLE Handle;
    UINTN                               NumberDevicePathNode;
    CHAR16                              *FilePath;

    //Do a sanity check on the Device file path
    if (DeviceFilePath == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    //  Convert the Device Path String into Device Path Protocol
    Status = gBS->LocateProtocol(&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol);
    ASSERT_EFI_ERROR(Status);
    DevicePath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath(DeviceFilePath);

    //Do a sanity check on the Device Path
    if (DevicePath == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    // Count the number of DevicePath Node
    NumberDevicePathNode = NumberNodeFromDevicePath(DevicePath);
    // Extract the FilePath from the Device Path
    FilePath = BdsExtractFilePathFromDevicePath(DeviceFilePath,NumberDevicePathNode);

    Status = BdsLoadDevicePath(DevicePath,&Handle);
    if (EFI_ERROR (Status)) {
        return Status;
    }

    //If FilePath == NULL then let consider if a MemoryMap Device Path
    if (FilePath == NULL) {
        // Check if the Node is a MemoryMap Device Path
        Status = BdsLoadFileFromMemMap(Handle,DevicePath,File);
    } else {
        Status = BdsLoadFileFromSimpleFileSystem(Handle,FilePath,File);
        if (EFI_ERROR (Status)) {
            Status = BdsLoadFileFromFirmwareVolume(Handle,FilePath,EFI_FV_FILETYPE_ALL,File);
        }
    }

    if (!EFI_ERROR (Status)) {
        File->DevicePath = DevicePath;
    }

    return Status;
}

EFI_STATUS BdsCopyRawFileToRuntimeMemory(
    IN  BDS_FILE            *File,
    OUT VOID                **FileImage,
    OUT UINTN               *FileSize
) {
    if (File == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    if (File->Type == BDS_FILETYPE_FS) {
        return BdsCopyRawFileToRuntimeMemoryFS(File->File.Fs.Handle,FileImage,FileSize);
    } else if (File->Type == BDS_FILETYPE_FV) {
        return BdsCopyRawFileToRuntimeMemoryFV(&(File->File.Fv),FileImage,FileSize);
    } else if (File->Type == BDS_FILETYPE_MEM) {
        return BdsCopyRawFileToRuntimeMemoryMemMap(&(File->File.Mem),FileImage,FileSize);
    } else {
        return EFI_INVALID_PARAMETER;
    }
}