[5d8d71e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup genarch
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <typedefs.h>
|
---|
[1f5c9c96] | 36 | #include <genarch/multiboot/multiboot.h>
|
---|
[5d8d71e] | 37 | #include <config.h>
|
---|
[19f857a] | 38 | #include <str.h>
|
---|
[5d8d71e] | 39 |
|
---|
| 40 | /** Extract command name from the multiboot module command line.
|
---|
| 41 | *
|
---|
[1f5c9c96] | 42 | * @param buf Destination buffer (will be always NULL-terminated).
|
---|
| 43 | * @param size Size of destination buffer (in bytes).
|
---|
[5d8d71e] | 44 | * @param cmd_line Input string (the command line).
|
---|
| 45 | *
|
---|
| 46 | */
|
---|
[1f5c9c96] | 47 | void multiboot_extract_command(char *buf, size_t size, const char *cmd_line)
|
---|
[5d8d71e] | 48 | {
|
---|
| 49 | /* Find the first space. */
|
---|
[b60c582] | 50 | const char *end = str_chr(cmd_line, ' ');
|
---|
[5d8d71e] | 51 | if (end == NULL)
|
---|
[06b785f] | 52 | end = cmd_line + str_size(cmd_line);
|
---|
[5d8d71e] | 53 |
|
---|
| 54 | /*
|
---|
| 55 | * Find last occurence of '/' before 'end'. If found, place start at
|
---|
| 56 | * next character. Otherwise, place start at beginning of buffer.
|
---|
| 57 | */
|
---|
[b60c582] | 58 | const char *cp = end;
|
---|
| 59 | const char *start = buf;
|
---|
| 60 |
|
---|
[5d8d71e] | 61 | while (cp != start) {
|
---|
| 62 | if (*cp == '/') {
|
---|
| 63 | start = cp + 1;
|
---|
| 64 | break;
|
---|
| 65 | }
|
---|
[b60c582] | 66 | cp--;
|
---|
[5d8d71e] | 67 | }
|
---|
| 68 |
|
---|
[b60c582] | 69 | /* Copy the command. */
|
---|
[1f5c9c96] | 70 | str_ncpy(buf, size, start, (size_t) (end - start));
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[c8cbd39] | 73 | /** Extract arguments from the multiboot module command line.
|
---|
| 74 | *
|
---|
| 75 | * @param buf Destination buffer (will be always NULL-terminated).
|
---|
| 76 | * @param size Size of destination buffer (in bytes).
|
---|
| 77 | * @param cmd_line Input string (the command line).
|
---|
| 78 | *
|
---|
| 79 | */
|
---|
| 80 | void multiboot_extract_argument(char *buf, size_t size, const char *cmd_line)
|
---|
| 81 | {
|
---|
| 82 | /* Start after first space. */
|
---|
| 83 | const char *start = str_chr(cmd_line, ' ');
|
---|
| 84 | if (start == NULL) {
|
---|
| 85 | str_cpy(buf, size, "");
|
---|
| 86 | return;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | const char *end = cmd_line + str_size(cmd_line);
|
---|
| 90 |
|
---|
| 91 | /* Skip the space(s). */
|
---|
| 92 | while (start != end) {
|
---|
| 93 | if (start[0] == ' ')
|
---|
| 94 | start++;
|
---|
| 95 | else
|
---|
| 96 | break;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | str_ncpy(buf, size, start, (size_t) (end - start));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[1f5c9c96] | 102 | static void multiboot_modules(uint32_t count, multiboot_module_t *mods)
|
---|
| 103 | {
|
---|
| 104 | for (uint32_t i = 0; i < count; i++) {
|
---|
| 105 | if (init.cnt >= CONFIG_INIT_TASKS)
|
---|
| 106 | break;
|
---|
| 107 |
|
---|
[32817cc] | 108 | init.tasks[init.cnt].paddr = mods[i].start;
|
---|
[1f5c9c96] | 109 | init.tasks[init.cnt].size = mods[i].end - mods[i].start;
|
---|
| 110 |
|
---|
| 111 | /* Copy command line, if available. */
|
---|
| 112 | if (mods[i].string) {
|
---|
| 113 | multiboot_extract_command(init.tasks[init.cnt].name,
|
---|
| 114 | CONFIG_TASK_NAME_BUFLEN, MULTIBOOT_PTR(mods[i].string));
|
---|
[c8cbd39] | 115 | multiboot_extract_argument(init.tasks[init.cnt].arguments,
|
---|
| 116 | CONFIG_TASK_ARGUMENTS_BUFLEN, MULTIBOOT_PTR(mods[i].string));
|
---|
| 117 | } else {
|
---|
[1f5c9c96] | 118 | init.tasks[init.cnt].name[0] = 0;
|
---|
[c8cbd39] | 119 | init.tasks[init.cnt].arguments[0] = 0;
|
---|
| 120 | }
|
---|
[1f5c9c96] | 121 |
|
---|
| 122 | init.cnt++;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | static void multiboot_memmap(uint32_t length, multiboot_memmap_t *memmap)
|
---|
| 127 | {
|
---|
| 128 | uint32_t pos = 0;
|
---|
| 129 |
|
---|
| 130 | while ((pos < length) && (e820counter < MEMMAP_E820_MAX_RECORDS)) {
|
---|
| 131 | e820table[e820counter] = memmap->mm_info;
|
---|
| 132 |
|
---|
| 133 | /* Compute address of next structure. */
|
---|
| 134 | uint32_t size = sizeof(memmap->size) + memmap->size;
|
---|
| 135 | memmap = (multiboot_memmap_t *) ((uintptr_t) memmap + size);
|
---|
| 136 | pos += size;
|
---|
| 137 |
|
---|
| 138 | e820counter++;
|
---|
| 139 | }
|
---|
[5d8d71e] | 140 | }
|
---|
| 141 |
|
---|
| 142 | /** Parse multiboot information structure.
|
---|
| 143 | *
|
---|
| 144 | * If @a signature does not contain a valid multiboot signature,
|
---|
| 145 | * assumes no multiboot information is available.
|
---|
| 146 | *
|
---|
| 147 | * @param signature Should contain the multiboot signature.
|
---|
[1f5c9c96] | 148 | * @param info Multiboot information structure.
|
---|
| 149 | *
|
---|
[5d8d71e] | 150 | */
|
---|
[1f5c9c96] | 151 | void multiboot_info_parse(uint32_t signature, const multiboot_info_t *info)
|
---|
[5d8d71e] | 152 | {
|
---|
[1f5c9c96] | 153 | if (signature != MULTIBOOT_LOADER_MAGIC)
|
---|
| 154 | return;
|
---|
[5d8d71e] | 155 |
|
---|
[1f5c9c96] | 156 | /* Copy modules information. */
|
---|
| 157 | if ((info->flags & MULTIBOOT_INFO_FLAGS_MODS) != 0)
|
---|
| 158 | multiboot_modules(info->mods_count,
|
---|
| 159 | (multiboot_module_t *) MULTIBOOT_PTR(info->mods_addr));
|
---|
[5d8d71e] | 160 |
|
---|
| 161 | /* Copy memory map. */
|
---|
[1f5c9c96] | 162 | if ((info->flags & MULTIBOOT_INFO_FLAGS_MMAP) != 0)
|
---|
| 163 | multiboot_memmap(info->mmap_length,
|
---|
| 164 | (multiboot_memmap_t *) MULTIBOOT_PTR(info->mmap_addr));
|
---|
[5d8d71e] | 165 | }
|
---|
| 166 |
|
---|
| 167 | /** @}
|
---|
| 168 | */
|
---|