paul@349 | 1 | /* |
paul@349 | 2 | * Support for initialising program memory regions in new tasks. |
paul@349 | 3 | * |
paul@349 | 4 | * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> |
paul@349 | 5 | * |
paul@349 | 6 | * This program is free software; you can redistribute it and/or |
paul@349 | 7 | * modify it under the terms of the GNU General Public License as |
paul@349 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@349 | 9 | * the License, or (at your option) any later version. |
paul@349 | 10 | * |
paul@349 | 11 | * This program is distributed in the hope that it will be useful, |
paul@349 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@349 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@349 | 14 | * GNU General Public License for more details. |
paul@349 | 15 | * |
paul@349 | 16 | * You should have received a copy of the GNU General Public License |
paul@349 | 17 | * along with this program; if not, write to the Free Software |
paul@349 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@349 | 19 | * Boston, MA 02110-1301, USA |
paul@349 | 20 | */ |
paul@349 | 21 | |
paul@349 | 22 | #include <l4/sys/err.h> |
paul@349 | 23 | #include <l4/util/elf.h> |
paul@349 | 24 | |
paul@349 | 25 | #include <fsclient/client.h> |
paul@349 | 26 | #include <systypes/fcntl.h> |
paul@349 | 27 | |
paul@349 | 28 | #include <string.h> |
paul@349 | 29 | |
paul@349 | 30 | #include "memory.h" |
paul@349 | 31 | |
paul@349 | 32 | |
paul@349 | 33 | |
paul@349 | 34 | /* Obtain the payload as a dataspace. */ |
paul@349 | 35 | |
paul@349 | 36 | long exec_get_payload(const char *filename, Payload **payload) |
paul@349 | 37 | { |
paul@349 | 38 | file_t *file = client_open(filename, O_RDONLY); |
paul@349 | 39 | |
paul@349 | 40 | if (file == NULL) |
paul@349 | 41 | return -L4_EIO; |
paul@349 | 42 | |
paul@349 | 43 | /* Obtain metadata from the file. */ |
paul@349 | 44 | |
paul@349 | 45 | char *buf = (char *) client_mmap(file, 0, file->size, 0, 0, L4RE_DS_F_R); |
paul@349 | 46 | |
paul@349 | 47 | /* Test the file type indicator. */ |
paul@349 | 48 | |
paul@349 | 49 | if ((file->size < EI_NIDENT) || memcmp(buf, "\x7f" "ELF", 4)) |
paul@349 | 50 | return -L4_EINVAL; |
paul@349 | 51 | |
paul@349 | 52 | /* Attempt to get a payload object appropriate for a particular object size |
paul@349 | 53 | variant. */ |
paul@349 | 54 | |
paul@349 | 55 | *payload = get_payload(buf); |
paul@349 | 56 | |
paul@349 | 57 | if ((*payload == NULL) || |
paul@349 | 58 | (file->size < (*payload)->header_extent()) || |
paul@349 | 59 | (file->size < (*payload)->program_header_extent())) |
paul@349 | 60 | return -L4_ERANGE; |
paul@349 | 61 | |
paul@349 | 62 | /* Obtain all loadable segments. */ |
paul@349 | 63 | |
paul@349 | 64 | for (unsigned int i = 0; i < (*payload)->segments(); i++) |
paul@349 | 65 | { |
paul@349 | 66 | Segment *segment = (*payload)->segment(i); |
paul@349 | 67 | long err; |
paul@349 | 68 | |
paul@349 | 69 | if (!segment->loadable()) |
paul@349 | 70 | continue; |
paul@349 | 71 | |
paul@349 | 72 | if (segment->file_contents()) |
paul@349 | 73 | { |
paul@349 | 74 | file_t *rfile = client_open(filename, file_opening_flags(segment->region_flags())); |
paul@349 | 75 | |
paul@349 | 76 | if (rfile == NULL) |
paul@349 | 77 | return -L4_EIO; |
paul@349 | 78 | |
paul@349 | 79 | err = segment->fill(rfile); |
paul@349 | 80 | } |
paul@349 | 81 | else |
paul@349 | 82 | err = segment->allocate(); |
paul@349 | 83 | |
paul@349 | 84 | if (err) |
paul@349 | 85 | return err; |
paul@349 | 86 | } |
paul@349 | 87 | |
paul@349 | 88 | return L4_EOK; |
paul@349 | 89 | } |
paul@349 | 90 | |
paul@349 | 91 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@349 | 92 | */ |