/src/sleuthkit/tsk/vs/mac.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * The Sleuth Kit  | 
3  |  |  *  | 
4  |  |  * Brian Carrier [carrier <at> sleuthkit [dot] org]  | 
5  |  |  * Copyright (c) 2006-2011 Brian Carrier, Basis Technology.  All rights reserved  | 
6  |  |  * Copyright (c) 2003-2005 Brian Carrier.  All rights reserved  | 
7  |  |  *  | 
8  |  |  * This software is distributed under the Common Public License 1.0  | 
9  |  |  */  | 
10  |  |  | 
11  |  | /** \file mac.c  | 
12  |  |  * Contains the internal functions to process and load a Mac partition table.  | 
13  |  |  */  | 
14  |  | #include "tsk_vs_i.h"  | 
15  |  | #include "tsk_mac.h"  | 
16  |  |  | 
17  |  |  | 
18  |  | /*  | 
19  |  |  * Process the partition table at the sector address  | 
20  |  |  *  | 
21  |  |  * It is loaded into the internal sorted list  | 
22  |  |  *  | 
23  |  |  * Return 1 on error and 0 on success  | 
24  |  |  */  | 
25  |  | static uint8_t  | 
26  |  | mac_load_table(TSK_VS_INFO * vs)  | 
27  | 976  | { | 
28  | 976  |     char *part_buf;  | 
29  | 976  |     mac_part *part;  | 
30  | 976  |     char *table_str;  | 
31  | 976  |     uint32_t idx, max_part;  | 
32  | 976  |     TSK_DADDR_T taddr = vs->offset / vs->block_size + MAC_PART_SOFFSET;  | 
33  | 976  |     TSK_DADDR_T max_addr = (vs->img_info->size - vs->offset) / vs->block_size;  // max sector  | 
34  |  |  | 
35  | 976  |     if (tsk_verbose)  | 
36  | 0  |         tsk_fprintf(stderr, "mac_load_table: Sector: %" PRIuDADDR "\n",  | 
37  | 0  |             taddr);  | 
38  |  |  | 
39  |  |     /* The table can be variable length, so we loop on it  | 
40  |  |      * The idx variable shows which round it is  | 
41  |  |      * Each structure is a block size  | 
42  |  |      */  | 
43  | 976  |     if ((part_buf = tsk_malloc(vs->block_size)) == NULL)  | 
44  | 0  |         return 1;  | 
45  | 976  |     part = (mac_part *) part_buf;  | 
46  |  |  | 
47  | 976  |     max_part = 1;               /* set it to 1 and it will be set in the first loop */  | 
48  | 84.8k  |     for (idx = 0; idx < max_part; idx++) { | 
49  | 84.6k  |         uint32_t part_start;  | 
50  | 84.6k  |         uint32_t part_size;  | 
51  | 84.6k  |         uint32_t part_status;  | 
52  | 84.6k  |         char *str;  | 
53  | 84.6k  |         ssize_t cnt;  | 
54  | 84.6k  |         int flag = 0;  | 
55  |  |  | 
56  |  |  | 
57  |  |         /* Read the entry */  | 
58  | 84.6k  |         cnt = tsk_vs_read_block  | 
59  | 84.6k  |             (vs, MAC_PART_SOFFSET + idx, part_buf, vs->block_size);  | 
60  |  |  | 
61  |  |         /* If -1, then tsk_errno is already set */  | 
62  | 84.6k  |         if (cnt != vs->block_size) { | 
63  | 487  |             if (cnt >= 0) { | 
64  | 182  |                 tsk_error_reset();  | 
65  | 182  |                 tsk_error_set_errno(TSK_ERR_VS_READ);  | 
66  | 182  |             }  | 
67  | 487  |             tsk_error_set_errstr2("MAC Partition entry %" PRIuDADDR, | 
68  | 487  |                 taddr + idx);  | 
69  | 487  |             free(part_buf);  | 
70  | 487  |             return 1;  | 
71  | 487  |         }  | 
72  |  |  | 
73  |  |  | 
74  |  |         /* Sanity Check */  | 
75  | 84.1k  |         if (idx == 0) { | 
76  |  |             /* Set the endian ordering the first time around */  | 
77  | 753  |             if (tsk_vs_guessu16(vs, part->magic, MAC_MAGIC)) { | 
78  | 77  |                 tsk_error_reset();  | 
79  | 77  |                 tsk_error_set_errno(TSK_ERR_VS_MAGIC);  | 
80  | 77  |                 tsk_error_set_errstr("Mac partition table entry (Sector: %" | 
81  | 77  |                     PRIuDADDR ") %" PRIx16,  | 
82  | 77  |                     (taddr + idx), tsk_getu16(vs->endian, part->magic));  | 
83  | 77  |                 if (tsk_verbose)  | 
84  | 0  |                     tsk_fprintf(stderr,  | 
85  | 0  |                         "mac_load: Missing initial magic value\n");  | 
86  | 77  |                 free(part_buf);  | 
87  | 77  |                 return 1;  | 
88  | 77  |             }  | 
89  |  |  | 
90  |  |             /* Get the number of partitions */  | 
91  | 676  |             max_part = tsk_getu32(vs->endian, part->pmap_size);  | 
92  | 676  |         }  | 
93  | 83.3k  |         else if (tsk_getu16(vs->endian, part->magic) != MAC_MAGIC) { | 
94  | 128  |             tsk_error_reset();  | 
95  | 128  |             tsk_error_set_errno(TSK_ERR_VS_MAGIC);  | 
96  | 128  |             tsk_error_set_errstr("Mac partition table entry (Sector: %" | 
97  | 128  |                 PRIuDADDR ") %" PRIx16, (taddr + idx),  | 
98  | 128  |                 tsk_getu16(vs->endian, part->magic));  | 
99  | 128  |             if (tsk_verbose)  | 
100  | 0  |                 tsk_fprintf(stderr,  | 
101  | 0  |                     "mac_load: Missing magic value in entry %" PRIu32 "\n",  | 
102  | 0  |                     idx);  | 
103  | 128  |             free(part_buf);  | 
104  | 128  |             return 1;  | 
105  | 128  |         }  | 
106  |  |  | 
107  |  |  | 
108  | 83.9k  |         part_start = tsk_getu32(vs->endian, part->start_sec);  | 
109  | 83.9k  |         part_size = tsk_getu32(vs->endian, part->size_sec);  | 
110  | 83.9k  |         part_status = tsk_getu32(vs->endian, part->status);  | 
111  |  |  | 
112  | 83.9k  |         if (tsk_verbose)  | 
113  | 0  |             tsk_fprintf(stderr,  | 
114  | 0  |                 "mac_load: %" PRIu32 "  Starting Sector: %" PRIu32  | 
115  | 0  |                 "  Size: %" PRIu32 " Type: %s Status: %"PRIu32"\n", idx, part_start,  | 
116  | 0  |                 part_size, part->type, part_status);  | 
117  |  |  | 
118  | 83.9k  |         if (part_size == 0)  | 
119  | 6.89k  |             continue;  | 
120  |  |  | 
121  | 77.0k  |         if (part_status == 0)  | 
122  | 1.25k  |             flag = TSK_VS_PART_FLAG_UNALLOC;  | 
123  | 75.7k  |         else  | 
124  | 75.7k  |             flag = TSK_VS_PART_FLAG_ALLOC;  | 
125  |  |  | 
126  |  |         // make sure the first couple are within the bounds of the image.  | 
127  | 77.0k  |         if ((idx < 2) && (part_start > max_addr)) { | 
128  | 61  |             tsk_error_reset();  | 
129  | 61  |             tsk_error_set_errno(TSK_ERR_VS_BLK_NUM);  | 
130  | 61  |             tsk_error_set_errstr  | 
131  | 61  |                 ("mac_load_table: Starting sector too large for image"); | 
132  | 61  |             if (tsk_verbose)  | 
133  | 0  |                 tsk_fprintf(stderr,  | 
134  | 0  |                     "mac_load: Starting sector too large for image (%"  | 
135  | 0  |                     PRIu32 " vs %" PRIu32 ")\n", part_start, max_addr);  | 
136  | 61  |             free(part_buf);  | 
137  | 61  |             return 1;  | 
138  | 61  |         }  | 
139  |  |  | 
140  |  |  | 
141  | 76.9k  |         if ((str = tsk_malloc(sizeof(part->name))) == NULL) { | 
142  | 0  |             free(part_buf);  | 
143  | 0  |             return 1;  | 
144  | 0  |         }  | 
145  |  |  | 
146  | 76.9k  |         strncpy(str, (char *) part->type, sizeof(part->name));  | 
147  | 76.9k  |         str[sizeof(part->name) - 1] = 0;  | 
148  |  |  | 
149  | 76.9k  |         if (NULL == tsk_vs_part_add(vs, (TSK_DADDR_T) part_start,  | 
150  | 76.9k  |                 (TSK_DADDR_T) part_size, (TSK_VS_PART_FLAG_ENUM)flag, str, -1,  | 
151  | 76.9k  |                 idx)) { | 
152  | 0  |             free(part_buf);  | 
153  | 0  |             return 1;  | 
154  | 0  |         }  | 
155  | 76.9k  |     }  | 
156  | 223  |     free(part_buf);  | 
157  | 223  |     part_buf = NULL;  | 
158  |  |  | 
159  |  |     // Bail if we didn't find any valid entries  | 
160  | 223  |     if (vs->part_count == 0) { | 
161  | 4  |         return 1;  | 
162  | 4  |     }  | 
163  |  |  | 
164  |  |     /* Add an entry for the table length */  | 
165  | 219  |     if ((table_str = tsk_malloc(16)) == NULL) { | 
166  | 0  |         return 1;  | 
167  | 0  |     }  | 
168  |  |  | 
169  | 219  |     snprintf(table_str, 16, "Table");  | 
170  | 219  |     if (NULL == tsk_vs_part_add(vs, taddr, max_part, TSK_VS_PART_FLAG_META,  | 
171  | 219  |             table_str, -1, -1)) { | 
172  | 0  |         return 1;  | 
173  | 0  |     }  | 
174  |  |  | 
175  | 219  |     return 0;  | 
176  | 219  | }  | 
177  |  |  | 
178  |  |  | 
179  |  | static void  | 
180  |  | mac_close(TSK_VS_INFO * vs)  | 
181  | 565  | { | 
182  | 565  |     vs->tag = 0;  | 
183  | 565  |     tsk_vs_part_free(vs);  | 
184  | 565  |     free(vs);  | 
185  | 565  | }  | 
186  |  |  | 
187  |  | /*  | 
188  |  |  * Process img_info as a Mac disk.  Initialize TSK_VS_INFO or return  | 
189  |  |  * NULL on error  | 
190  |  |  * */  | 
191  |  | TSK_VS_INFO *  | 
192  |  | tsk_vs_mac_open(TSK_IMG_INFO * img_info, TSK_DADDR_T offset)  | 
193  | 565  | { | 
194  | 565  |     TSK_VS_INFO *vs;  | 
195  |  |  | 
196  |  |     // clean up any errors that are lying around  | 
197  | 565  |     tsk_error_reset();  | 
198  |  |  | 
199  | 565  |     if (img_info->sector_size == 0) { | 
200  | 0  |         tsk_error_reset();  | 
201  | 0  |         tsk_error_set_errno(TSK_ERR_VS_ARG);  | 
202  | 0  |         tsk_error_set_errstr("tsk_vs_mac_open: sector size is 0"); | 
203  | 0  |         return NULL;  | 
204  | 0  |     }  | 
205  |  |  | 
206  | 565  |     vs = (TSK_VS_INFO *) tsk_malloc(sizeof(*vs));  | 
207  | 565  |     if (vs == NULL)  | 
208  | 0  |         return NULL;  | 
209  |  |  | 
210  | 565  |     vs->img_info = img_info;  | 
211  | 565  |     vs->vstype = TSK_VS_TYPE_MAC;  | 
212  | 565  |     vs->tag = TSK_VS_INFO_TAG;  | 
213  |  |  | 
214  |  |     /* If an offset was given, then use that too */  | 
215  | 565  |     vs->offset = offset;  | 
216  |  |  | 
217  |  |     //vs->sect_offset = offset + MAC_PART_OFFSET;  | 
218  |  |  | 
219  |  |     /* initialize settings */  | 
220  | 565  |     vs->part_list = NULL;  | 
221  | 565  |     vs->part_count = 0;  | 
222  | 565  |     vs->endian = 0;  | 
223  | 565  |     vs->block_size = img_info->sector_size;  | 
224  |  |  | 
225  |  |     /* Assign functions */  | 
226  | 565  |     vs->close = mac_close;  | 
227  |  |  | 
228  |  |     /* Load the partitions into the sorted list */  | 
229  | 565  |     if (mac_load_table(vs)) { | 
230  |  |  | 
231  |  |         // try some other sector sizes  | 
232  | 411  |         uint8_t returnNull = 1;  | 
233  | 411  |         if (vs->block_size == 512) { | 
234  | 411  |             if (tsk_verbose)  | 
235  | 0  |                 tsk_fprintf(stderr,  | 
236  | 0  |                     "mac_open: Trying 4096-byte sector size instead of 512-byte\n");  | 
237  | 411  |             vs->block_size = 4096;  | 
238  | 411  |             returnNull = mac_load_table(vs);  | 
239  | 411  |         }  | 
240  | 0  |         else if (vs->block_size == 4096) { | 
241  | 0  |             if (tsk_verbose)  | 
242  | 0  |                 tsk_fprintf(stderr,  | 
243  | 0  |                     "mac_open: Trying 512-byte sector size instead of 4096-byte\n");  | 
244  | 0  |             vs->block_size = 512;  | 
245  | 0  |             returnNull = mac_load_table(vs);  | 
246  | 0  |         }  | 
247  |  |  | 
248  | 411  |         if (returnNull) { | 
249  | 346  |             mac_close(vs);  | 
250  | 346  |             return NULL;  | 
251  | 346  |         }  | 
252  | 411  |     }  | 
253  |  |  | 
254  |  |     /* fill in the sorted list with the 'unknown' values */  | 
255  | 219  |     if (tsk_vs_part_unused(vs)) { | 
256  | 0  |         mac_close(vs);  | 
257  | 0  |         return NULL;  | 
258  | 0  |     }  | 
259  |  |  | 
260  | 219  |     return vs;  | 
261  | 219  | }  |