1 /*
2 
3     File: chgtype.c
4 
5     Copyright (C) 1998-2009 Christophe GRENIER <grenier@cgsecurity.org>
6 
7     This software is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write the Free Software Foundation, Inc., 51
19     Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21  */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <stdio.h>
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30 #include <assert.h>
31 #include "types.h"
32 #include "common.h"
33 #include "chgtype.h"
34 #include "log.h"
35 #include "log_part.h"
36 
37 extern const arch_fnct_t arch_gpt;
38 extern const arch_fnct_t arch_none;
39 
get_hex_from_command(char ** current_cmd)40 static int get_hex_from_command(char **current_cmd)
41 {
42   const int tmp=strtol(*current_cmd, NULL, 16);
43   while(*current_cmd[0]!=',' && *current_cmd[0]!='\0')
44     (*current_cmd)++;
45   return tmp;
46 }
47 
change_part_type_cli(const disk_t * disk_car,partition_t * partition,char ** current_cmd)48 void change_part_type_cli(const disk_t *disk_car,partition_t *partition, char **current_cmd)
49 {
50   assert(current_cmd!=NULL);
51   assert(partition!=NULL);
52   if(*current_cmd==NULL || partition->arch==NULL)
53     return ;
54   if(partition->arch==NULL)
55     return;
56   if(partition->arch==&arch_gpt)
57   {
58     partition->arch=&arch_none;
59     skip_comma_in_command(current_cmd);
60     {
61       const int tmp_val=get_hex_from_command(current_cmd);
62       partition->arch->set_part_type(partition,tmp_val);
63     }
64     log_info("Change partition type:\n");
65     log_partition(disk_car,partition);
66     partition->arch=&arch_gpt;
67     return;
68   }
69   if(partition->arch->set_part_type==NULL)
70     return ;
71   skip_comma_in_command(current_cmd);
72   {
73     const int tmp_val=get_hex_from_command(current_cmd);
74     partition->arch->set_part_type(partition,tmp_val);
75   }
76   log_info("Change partition type:\n");
77   log_partition(disk_car,partition);
78   return ;
79 }
80