1 /*
   2  *  GRUB  --  GRand Unified Bootloader
   3  *  Copyright (C) 2012 Daniil Lunev
   4  *
   5  *  GRUB is free software: you can redistribute it and/or modify
   6  *  it under the terms of the GNU General Public License as published by
   7  *  the Free Software Foundation, either version 3 of the License, or
   8  *  (at your option) any later version.
   9  *
  10  *  GRUB is distributed in the hope that it will be useful,
  11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  *  GNU General Public License for more details.
  14  *
  15  *  You should have received a copy of the GNU General Public License
  16  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  17  */
  18 
  19 #include <grub/types.h>
  20 #include <grub/misc.h>
  21 #include <grub/command.h>
  22 #include <grub/mm.h>
  23 #include <grub/err.h>
  24 #include <grub/dl.h>
  25 #include <grub/file.h>
  26 #include <grub/normal.h>
  27 #include <grub/script_sh.h>
  28 #include <grub/i18n.h>
  29 #include <grub/term.h>
  30 #include <grub/legacy_parse.h>
  31 #include <grub/crypto.h>
  32 #include <grub/auth.h>
  33 #include <grub/disk.h>
  34 #include <grub/partition.h>
  35 
  36 #include "menu_managing.c"
  37 
  38 GRUB_MOD_LICENSE ("GPLv3+");
  39 
  40 static entries *
  41 parse_entries(grub_file_t file)
  42 {
  43   entries * list = NULL;
  44   entries * current = NULL;
  45   init_entries(&list);
  46   for(;;) {
  47     char * buf = grub_file_getline(file);
  48     char * param = buf;
  49     char * value;
  50     
  51     if (! buf) 
  52       break;
  53   
  54     if ((buf[0] == '#') || (buf[0] == ' ') ||
  55       (buf[0] == '\t') || (buf[0] == '\n')) {
  56       grub_free(buf);
  57       continue;
  58     }
  59 
  60     while ((*param == ' ') || (*param == '\t'))
  61       ++param;
  62 
  63     value = grub_strchr(param, ' ');
  64     if (! value)
  65       value = grub_strchr(param, '\t');
  66 
  67     if (! value) {
  68       grub_free(buf);
  69       continue;
  70     }
  71 
  72     *value++ = 0;
  73     while ((*value == ' ') || (*value == '\t'))
  74       ++value;
  75 
  76     if ((*value == '\0') || (*value == '\n')) {
  77       grub_free(buf);
  78       continue;
  79     }
  80     
  81     if (! grub_strcmp(param, "default")) {
  82     } else if (! grub_strcmp(param, "timeout")) {
  83     } else if (! grub_strcmp(param, "serial")) {
  84     } else if (! grub_strcmp(param, "terminal")) {
  85     } else if (! grub_strcmp(param, "title")) {
  86       current = new_entry(value, list);
  87     } else if (! grub_strcmp(param, "bootfs")) {
  88       char * lpath;
  89       if (! current) {
  90         grub_free(buf);
  91         grub_error (GRUB_ERR_INVALID_COMMAND, N_("illumos syntax error"));
  92         return NULL;
  93       }
  94       lpath = grub_strchr(value, '/');
  95       *lpath = 0;
  96       grub_strcpy(current->entry_info[POOL_LABEL], value);
  97       *lpath = '/';
  98       grub_strcpy(current->entry_info[DATA_SET], lpath);
  99     } else if (! grub_strcmp(param, "kernel$")) {
 100       char * ender;
 101       ender = grub_strchr(value, ' ');
 102       if (! ender)  
 103         ender = grub_strchr(value, '\t');
 104       if (ender)
 105         *ender = 0;
 106       grub_strcpy(current->entry_info[KERNEL_PATH], value);
 107       if (ender) {
 108         ++ender;
 109         while ((*ender == ' ') || (*ender == '\t'))
 110           ++ender;
 111         grub_strcpy(current->entry_info[KERNEL_OPTIONS], ender);
 112       }
 113     } else if (! grub_strcmp(param, "module$")) {
 114       grub_strcpy(current->entry_info[BA_PATH], value);
 115     } else {
 116       grub_free(buf);
 117       continue;
 118     }
 119     grub_free(buf);
 120   }
 121   return list;
 122 }
 123 
 124 static grub_err_t
 125 grub_solaris_legacy_cmd(struct grub_command *cmd,
 126       int argc, char **args)
 127 {
 128   int new_env, extractor;
 129   extractor = (cmd->name[0] == 'e');
 130   new_env = (cmd->name[extractor ? (sizeof ("extract_slegacy_entries_") - 1)
 131            : (sizeof ("slegacy_") - 1)] == 'c');
 132 
 133 
 134   if (argc != 1)
 135     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
 136 
 137   grub_file_t file = grub_file_open(args[0]);
 138 
 139   if (new_env)
 140     grub_cls ();
 141 
 142   if (new_env && !extractor)
 143     grub_env_context_open ();
 144   if (extractor)
 145     grub_env_extractor_open (!new_env);
 146 
 147   if (! file)
 148     return grub_errno;
 149 
 150   entries * list = parse_entries(file);
 151 
 152   if (! list) {
 153     return grub_error (GRUB_ERR_INVALID_COMMAND, N_("illumos syntax error"));
 154   }
 155   
 156   add_entries(list);
 157 
 158   if (new_env)
 159   {
 160     grub_menu_t menu;
 161     menu = grub_env_get_menu ();
 162     if (menu && menu->size)
 163     grub_show_menu (menu, 1, 0);
 164     if (!extractor)
 165       grub_env_context_close ();
 166   }
 167   if (extractor)
 168     grub_env_extractor_close (!new_env);
 169 
 170   clear_entries(list);
 171 
 172   grub_file_close(file);
 173   return 0;
 174 }
 175 
 176 static grub_command_t cmd_source, cmd_configfile;
 177 static grub_command_t cmd_source_extract, cmd_configfile_extract;
 178 
 179 GRUB_MOD_INIT(solaris_legacy)
 180 {  
 181   cmd_source
 182     = grub_register_command ("slegacy_source",
 183            grub_solaris_legacy_cmd,
 184            N_("FILE"),
 185            /* TRANSLATORS: "legacy config" means
 186         "config as used by grub-legacy".  */
 187            N_("Parse legacy config in same context"));
 188   cmd_configfile
 189     = grub_register_command ("slegacy_configfile",
 190            grub_solaris_legacy_cmd,
 191            N_("FILE"),
 192            N_("Parse legacy config in new context"));
 193   cmd_source_extract
 194     = grub_register_command ("extract_slegacy_entries_source",
 195            grub_solaris_legacy_cmd,
 196            N_("FILE"),
 197            N_("Parse legacy config in same context taking only menu entries"));
 198   cmd_configfile_extract
 199     = grub_register_command ("extract_slegacy_entries_configfile",
 200            grub_solaris_legacy_cmd,
 201            N_("FILE"),
 202            N_("Parse legacy config in new context taking only menu entries"));
 203 
 204 
 205 }
 206 
 207 GRUB_MOD_FINI(solaris_legacy) 
 208 {
 209   grub_unregister_command (cmd_source);
 210   grub_unregister_command (cmd_configfile);
 211   grub_unregister_command (cmd_source_extract);
 212   grub_unregister_command (cmd_configfile_extract);
 213 }