summaryrefslogtreecommitdiff
path: root/efi/multifs.c
blob: 2f217d9dc2d32c09912501ce0ec76efbe5ee1778 (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
/*
 * Copyright (c) 2015 Paulo Alcantara <pcacjr@zytor.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <fs.h>
#include <multifs.h>

#include "efi.h"

static EFI_HANDLE *logical_parts = NULL;
static unsigned int logical_parts_no = 0;

/* Find all device handles which support EFI_BLOCK_IO_PROTOCOL and are logical
 * partitions */
static EFI_STATUS find_all_logical_parts(void)
{
    EFI_STATUS status;
    unsigned long len = 0;
    EFI_HANDLE *handles = NULL;
    unsigned long i;
    EFI_BLOCK_IO *bio;

    if (logical_parts) {
        status = EFI_SUCCESS;
        goto out;
    }

    status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol,
                               &BlockIoProtocol, NULL, &len, NULL);
    if (EFI_ERROR(status) && status != EFI_BUFFER_TOO_SMALL)
        goto out;

    handles = malloc(len);
    if (!handles) {
        status = EFI_OUT_OF_RESOURCES;
        goto out;
    }

    logical_parts = malloc(len);
    if (!logical_parts) {
        status = EFI_OUT_OF_RESOURCES;
        goto out_free;
    }

    status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol,
                               &BlockIoProtocol, NULL, &len,
                               (void **)handles);
    if (EFI_ERROR(status))
        goto out_free;

    for (i = 0; i < len / sizeof(EFI_HANDLE); i++) {
        status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
                                   &BlockIoProtocol, (void **)&bio);
        if (EFI_ERROR(status))
            goto out_free;
        if (bio->Media->LogicalPartition) {
            logical_parts[logical_parts_no++] = handles[i];
        }
    }

    free(handles);
    return status;

out_free:
    if (handles)
        free(handles);
    if (logical_parts)
        free(logical_parts);
out:
    return status;
}

static inline EFI_HANDLE get_logical_part(unsigned int partno)
{
    if (!logical_parts || partno > logical_parts_no)
        return NULL;
    return logical_parts[partno - 1];
}

static inline EFI_HANDLE find_device_handle(unsigned int diskno,
                                            unsigned int partno)
{
    return get_logical_part(partno);
}

static inline void *get_dev_info_priv(EFI_HANDLE handle)
{
    struct efi_disk_private *priv;

    priv = malloc(sizeof(*priv));
    if (!priv)
        return NULL;
    priv->dev_handle = handle;
    return priv;
}

__export struct fs_info *efi_multifs_get_fs_info(const char **path)
{
    uint8_t diskno;
    uint8_t partno;
    struct fs_info *fsp;
    EFI_HANDLE handle;
    void *priv;
    int ret;

    if (multifs_parse_path(path, &diskno, &partno))
        return NULL;

    fsp = multifs_get_fs(diskno, partno - 1);
    if (fsp)
        return fsp;

    fsp = malloc(sizeof(*fsp));
    if (!fsp)
        return NULL;

    handle = find_device_handle(diskno, partno);
    if (!handle)
        goto free_fsp;
    dprintf("%s: found partition %d\n", __func__, partno);

    priv = get_dev_info_priv(handle);
    if (!priv)
        goto free_fsp;

    ret = multifs_setup_fs_info(fsp, diskno, partno, priv);
    if (ret) {
        dprintf("%s: failed to set up fs info\n", __func__);
        goto free_priv;
    }
    return fsp;

free_priv:
    free(priv);
free_fsp:
    free(fsp);
    return NULL;
}

__export void efi_multifs_init(void *addr __attribute__((unused)))
{
    EFI_STATUS status;

    status = find_all_logical_parts();
    if (EFI_ERROR(status)) {
        printf("%s: failed to locate device handles of logical partitions\n",
               __func__);
        printf("%s: EFI status code: 0x%08X\n", __func__, status);
        return;
    }
    dprintf("%s: initialised multifs support\n", __func__);
}