Browse code

mbr: updates to gpt detection on mbr

Kevin Lin authored on 2014/03/14 01:47:40
Showing 4 changed files
... ...
@@ -316,16 +316,16 @@ cli_file_t cli_filetype2(fmap_t *map, const struct cl_engine *engine, cli_file_t
316 316
 
317 317
             /* raw dmgs must be a multiple of 512 */
318 318
             if ((map->len % 512) == 0 && map->len > 512) {
319
-                /* check if detected MBR is protective on GPT */
320
-                if (gpt_detect_size(map) != 0) {
321
-                    cli_dbgmsg("Recognized GUID Partition Table file\n");
322
-                    return CL_TYPE_GPT;
323
-                }
324
-
325 319
                 /* check if the MBR is a valid configuration */
326 320
                 if (cli_mbr_check(buff, bread, map->len) == 0) {
327 321
                     return CL_TYPE_MBR;
328 322
                 }
323
+
324
+                /* check if detected MBR is protective or hybridon GPT */
325
+                if (cli_mbr_check_gpt(buff, bread) != 0) {
326
+                    cli_dbgmsg("Recognized GUID Partition Table file\n");
327
+                    return CL_TYPE_GPT;
328
+                }
329 329
             }
330 330
 
331 331
             /* re-detect type */
... ...
@@ -80,18 +80,22 @@ size_t gpt_detect_size(fmap_t *map)
80 80
     unsigned char *buff;
81 81
 
82 82
     buff = (unsigned char*)fmap_need_off_once(map, 512, 8);
83
+    if (!buff) return 0;
83 84
     if (0 == strncmp(buff, GPT_SIGNATURE_STR, 8))
84 85
         return 512;
85 86
 
86 87
     buff = (unsigned char*)fmap_need_off_once(map, 1024, 8);
88
+    if (!buff) return 0;
87 89
     if (0 == strncmp(buff, GPT_SIGNATURE_STR, 8))
88 90
         return 1024;
89 91
 
90 92
     buff = (unsigned char*)fmap_need_off_once(map, 2048, 8);
93
+    if (!buff) return 0;
91 94
     if (0 == strncmp(buff, GPT_SIGNATURE_STR, 8))
92 95
         return 2048;
93 96
 
94 97
     buff = (unsigned char*)fmap_need_off_once(map, 4096, 8);
98
+    if (!buff) return 0;
95 99
     if (0 == strncmp(buff, GPT_SIGNATURE_STR, 8))
96 100
         return 4096;
97 101
 
... ...
@@ -94,6 +94,23 @@ int cli_mbr_check(const unsigned char *buff, size_t len, size_t maplen) {
94 94
     return mbr_check_mbr(&mbr, maplen, sectorsize);
95 95
 }
96 96
 
97
+int cli_mbr_check_gpt(const unsigned char *buff, size_t len) {
98
+    struct mbr_boot_record mbr;
99
+    off_t mbr_base = 0;
100
+    size_t sectorsize = 512;
101
+
102
+    if (len < sectorsize) {
103
+        return CL_EFORMAT;
104
+    }
105
+
106
+    mbr_base = sectorsize - sizeof(struct mbr_boot_record);
107
+    memcpy(&mbr, buff+mbr_base, sizeof(mbr));
108
+    mbr_convert_to_host(&mbr);
109
+
110
+    return ((mbr.entries[0].type == MBR_PROTECTIVE) || 
111
+            (mbr.entries[0].type == MBR_HYBRID));
112
+}
113
+
97 114
 /* sets sectorsize to default value if specfied to be 0 */
98 115
 int cli_scanmbr(cli_ctx *ctx, size_t sectorsize)
99 116
 {
... ...
@@ -85,6 +85,7 @@ struct mbr_boot_record {
85 85
 #endif
86 86
 
87 87
 int cli_mbr_check(const unsigned char *buff, size_t len, size_t maplen);
88
+int cli_mbr_check_gpt(const unsigned char *buff, size_t len);
88 89
 int cli_scanmbr(cli_ctx *ctx, size_t sectorsize);
89 90
 void mbr_convert_to_host(struct mbr_boot_record *record);
90 91