[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 |
|
---|
[6404aca] | 29 | /** @addtogroup kernel_genarch
|
---|
[5d8d71e] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <typedefs.h>
|
---|
[1f5c9c96] | 36 | #include <genarch/multiboot/multiboot.h>
|
---|
[5d8d71e] | 37 | #include <config.h>
|
---|
[e29e44bf] | 38 | #include <stddef.h>
|
---|
[19f857a] | 39 | #include <str.h>
|
---|
[5d8d71e] | 40 |
|
---|
| 41 | /** Extract command name from the multiboot module command line.
|
---|
| 42 | *
|
---|
[1f5c9c96] | 43 | * @param buf Destination buffer (will be always NULL-terminated).
|
---|
| 44 | * @param size Size of destination buffer (in bytes).
|
---|
[5d8d71e] | 45 | * @param cmd_line Input string (the command line).
|
---|
| 46 | *
|
---|
| 47 | */
|
---|
[1f5c9c96] | 48 | void multiboot_extract_command(char *buf, size_t size, const char *cmd_line)
|
---|
[5d8d71e] | 49 | {
|
---|
| 50 | /* Find the first space. */
|
---|
[b60c582] | 51 | const char *end = str_chr(cmd_line, ' ');
|
---|
[5d8d71e] | 52 | if (end == NULL)
|
---|
[06b785f] | 53 | end = cmd_line + str_size(cmd_line);
|
---|
[a35b458] | 54 |
|
---|
[5d8d71e] | 55 | /*
|
---|
| 56 | * Find last occurence of '/' before 'end'. If found, place start at
|
---|
| 57 | * next character. Otherwise, place start at beginning of buffer.
|
---|
| 58 | */
|
---|
[b60c582] | 59 | const char *cp = end;
|
---|
| 60 | const char *start = buf;
|
---|
[a35b458] | 61 |
|
---|
[5d8d71e] | 62 | while (cp != start) {
|
---|
| 63 | if (*cp == '/') {
|
---|
| 64 | start = cp + 1;
|
---|
| 65 | break;
|
---|
| 66 | }
|
---|
[b60c582] | 67 | cp--;
|
---|
[5d8d71e] | 68 | }
|
---|
[a35b458] | 69 |
|
---|
[b60c582] | 70 | /* Copy the command. */
|
---|
[1f5c9c96] | 71 | str_ncpy(buf, size, start, (size_t) (end - start));
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[c8cbd39] | 74 | /** Extract arguments from the multiboot module command line.
|
---|
| 75 | *
|
---|
| 76 | * @param buf Destination buffer (will be always NULL-terminated).
|
---|
| 77 | * @param size Size of destination buffer (in bytes).
|
---|
| 78 | * @param cmd_line Input string (the command line).
|
---|
| 79 | *
|
---|
| 80 | */
|
---|
| 81 | void multiboot_extract_argument(char *buf, size_t size, const char *cmd_line)
|
---|
| 82 | {
|
---|
| 83 | /* Start after first space. */
|
---|
| 84 | const char *start = str_chr(cmd_line, ' ');
|
---|
| 85 | if (start == NULL) {
|
---|
| 86 | str_cpy(buf, size, "");
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | const char *end = cmd_line + str_size(cmd_line);
|
---|
| 91 |
|
---|
| 92 | /* Skip the space(s). */
|
---|
| 93 | while (start != end) {
|
---|
| 94 | if (start[0] == ' ')
|
---|
| 95 | start++;
|
---|
| 96 | else
|
---|
| 97 | break;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | str_ncpy(buf, size, start, (size_t) (end - start));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[1f5c9c96] | 103 | static void multiboot_modules(uint32_t count, multiboot_module_t *mods)
|
---|
| 104 | {
|
---|
| 105 | for (uint32_t i = 0; i < count; i++) {
|
---|
| 106 | if (init.cnt >= CONFIG_INIT_TASKS)
|
---|
| 107 | break;
|
---|
[a35b458] | 108 |
|
---|
[32817cc] | 109 | init.tasks[init.cnt].paddr = mods[i].start;
|
---|
[1f5c9c96] | 110 | init.tasks[init.cnt].size = mods[i].end - mods[i].start;
|
---|
[a35b458] | 111 |
|
---|
[1f5c9c96] | 112 | /* Copy command line, if available. */
|
---|
| 113 | if (mods[i].string) {
|
---|
| 114 | multiboot_extract_command(init.tasks[init.cnt].name,
|
---|
| 115 | CONFIG_TASK_NAME_BUFLEN, MULTIBOOT_PTR(mods[i].string));
|
---|
[c8cbd39] | 116 | multiboot_extract_argument(init.tasks[init.cnt].arguments,
|
---|
| 117 | CONFIG_TASK_ARGUMENTS_BUFLEN, MULTIBOOT_PTR(mods[i].string));
|
---|
| 118 | } else {
|
---|
[1f5c9c96] | 119 | init.tasks[init.cnt].name[0] = 0;
|
---|
[c8cbd39] | 120 | init.tasks[init.cnt].arguments[0] = 0;
|
---|
| 121 | }
|
---|
[a35b458] | 122 |
|
---|
[1f5c9c96] | 123 | init.cnt++;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | static void multiboot_memmap(uint32_t length, multiboot_memmap_t *memmap)
|
---|
| 128 | {
|
---|
| 129 | uint32_t pos = 0;
|
---|
[a35b458] | 130 |
|
---|
[1f5c9c96] | 131 | while ((pos < length) && (e820counter < MEMMAP_E820_MAX_RECORDS)) {
|
---|
| 132 | e820table[e820counter] = memmap->mm_info;
|
---|
[a35b458] | 133 |
|
---|
[1f5c9c96] | 134 | /* Compute address of next structure. */
|
---|
| 135 | uint32_t size = sizeof(memmap->size) + memmap->size;
|
---|
| 136 | memmap = (multiboot_memmap_t *) ((uintptr_t) memmap + size);
|
---|
| 137 | pos += size;
|
---|
[a35b458] | 138 |
|
---|
[1f5c9c96] | 139 | e820counter++;
|
---|
| 140 | }
|
---|
[5d8d71e] | 141 | }
|
---|
| 142 |
|
---|
| 143 | /** Parse multiboot information structure.
|
---|
| 144 | *
|
---|
| 145 | * If @a signature does not contain a valid multiboot signature,
|
---|
| 146 | * assumes no multiboot information is available.
|
---|
| 147 | *
|
---|
| 148 | * @param signature Should contain the multiboot signature.
|
---|
[1f5c9c96] | 149 | * @param info Multiboot information structure.
|
---|
| 150 | *
|
---|
[5d8d71e] | 151 | */
|
---|
[1f5c9c96] | 152 | void multiboot_info_parse(uint32_t signature, const multiboot_info_t *info)
|
---|
[5d8d71e] | 153 | {
|
---|
[1f5c9c96] | 154 | if (signature != MULTIBOOT_LOADER_MAGIC)
|
---|
| 155 | return;
|
---|
[ba1ea40] | 156 |
|
---|
| 157 | /* Copy command line. */
|
---|
| 158 | if ((info->flags & MULTIBOOT_INFO_FLAGS_CMDLINE) != 0)
|
---|
| 159 | multiboot_cmdline((char *) MULTIBOOT_PTR(info->cmd_line));
|
---|
| 160 |
|
---|
[1f5c9c96] | 161 | /* Copy modules information. */
|
---|
| 162 | if ((info->flags & MULTIBOOT_INFO_FLAGS_MODS) != 0)
|
---|
| 163 | multiboot_modules(info->mods_count,
|
---|
| 164 | (multiboot_module_t *) MULTIBOOT_PTR(info->mods_addr));
|
---|
[a35b458] | 165 |
|
---|
[5d8d71e] | 166 | /* Copy memory map. */
|
---|
[1f5c9c96] | 167 | if ((info->flags & MULTIBOOT_INFO_FLAGS_MMAP) != 0)
|
---|
| 168 | multiboot_memmap(info->mmap_length,
|
---|
| 169 | (multiboot_memmap_t *) MULTIBOOT_PTR(info->mmap_addr));
|
---|
[5d8d71e] | 170 | }
|
---|
| 171 |
|
---|
| 172 | /** @}
|
---|
| 173 | */
|
---|