"Fossies" - the Fresh Open Source Software Archive

Member "ms-sys-2.7.0/src/br.c" (7 Jan 2016, 8324 Bytes) of package /linux/privat/ms-sys-2.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "br.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.4.1_vs_2.6.0.

    1 /******************************************************************
    2     Copyright (C) 2009-2015  Henrik Carlqvist
    3 
    4     This program is free software; you can redistribute it and/or modify
    5     it under the terms of the GNU General Public License as published by
    6     the Free Software Foundation; either version 2 of the License, or
    7     (at your option) any later version.
    8 
    9     This program is distributed in the hope that it will be useful,
   10     but WITHOUT ANY WARRANTY; without even the implied warranty of
   11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12     GNU General Public License for more details.
   13 
   14     You should have received a copy of the GNU General Public License
   15     along with this program; if not, write to the Free Software
   16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   17 ******************************************************************/
   18 #include <stdio.h>
   19 
   20 #include "file.h"
   21 #include "nls.h"
   22 #include "br.h"
   23 
   24 static unsigned long ulBytesPerSector = 512;
   25 
   26 void set_bytes_per_sector(unsigned long ulValue)
   27 {
   28    ulBytesPerSector = ulValue;
   29    if ((ulBytesPerSector < 512) || (ulBytesPerSector > 65536))
   30       ulBytesPerSector = 512;
   31 } /* set_bytes_per_sector */
   32 
   33 uint32_t read_windows_disk_signature(FILE *fp)
   34 {
   35    uint32_t tWDS;
   36    if(!read_data(fp, 0x1b8, &tWDS, 4))
   37       return 0;
   38    return tWDS;
   39 } /* read_windows_disk_signature */
   40 
   41 int write_windows_disk_signature(FILE *fp, uint32_t tWDS)
   42 {
   43    return write_data(fp, 0x1b8, &tWDS, 4);
   44 } /* write_windows_disk_signature */
   45 
   46 uint16_t read_mbr_copy_protect_bytes(FILE *fp)
   47 {
   48    uint16_t tOut;
   49    if(!read_data(fp, 0x1bc, &tOut, 2))
   50       return 0xffff;
   51    return tOut;
   52 } /* read_mbr_copy_protect_bytes */
   53 
   54 const char *read_mbr_copy_protect_bytes_explained(FILE *fp)
   55 {
   56    uint16_t t = read_mbr_copy_protect_bytes(fp);
   57    switch(t)
   58    {
   59       case 0:
   60      return _("not copy protected");
   61       case 0x5a5a:
   62      return _("copy protected");
   63       default:
   64      return _("unknown value");
   65    }
   66 } /* read_mbr_copy_protect_bytes_explained */
   67 
   68 int is_br(FILE *fp)
   69 {
   70    /* A "file" is probably some kind of boot record if it contains the magic
   71       chars 0x55, 0xAA at position 0x1FE */
   72    unsigned char aucRef[] = {0x55, 0xAA};
   73 
   74    return contains_data(fp, 0x1FE, aucRef, sizeof(aucRef)); 
   75 } /* is_br */
   76 
   77 int is_lilo_br(FILE *fp)
   78 {
   79    /* A "file" is probably a LILO boot record if it contains the magic
   80       chars LILO at position 0x6 or 0x2 for floppies */
   81    unsigned char aucRef[] = {'L','I','L','O'};
   82 
   83    return ( contains_data(fp, 0x6, aucRef, sizeof(aucRef)) ||
   84         contains_data(fp, 0x2, aucRef, sizeof(aucRef)) ); 
   85 } /* is_lilo_br */
   86 
   87 int is_dos_mbr(FILE *fp)
   88 {
   89    #include "mbr_dos.h"
   90 
   91    return
   92       contains_data(fp, 0x0, mbr_dos_0x0, sizeof(mbr_dos_0x0)) &&
   93       is_br(fp);
   94 } /* is_dos_mbr */
   95 
   96 int is_dos_f2_mbr(FILE *fp)
   97 {
   98    #include "mbr_dos_f2.h"
   99 
  100    return
  101       contains_data(fp, 0x0, mbr_dos_f2_0x0, sizeof(mbr_dos_f2_0x0)) &&
  102       is_br(fp);
  103 } /* is_dos_f2_mbr */
  104 
  105 int is_95b_mbr(FILE *fp)
  106 {
  107    #include "mbr_95b.h"
  108 
  109    return
  110       contains_data(fp, 0x0,   mbr_95b_0x0,   sizeof(mbr_95b_0x0)) &&
  111       contains_data(fp, 0x0e0, mbr_95b_0x0e0, sizeof(mbr_95b_0x0e0)) &&
  112       is_br(fp);
  113 } /* is_95b_mbr */
  114 
  115 int is_2000_mbr(FILE *fp)
  116 {
  117    #include "mbr_2000.h"
  118 
  119    return
  120       contains_data(fp, 0x0, mbr_2000_0x0, sizeof(mbr_2000_0x0)) &&
  121       is_br(fp);
  122 } /* is_2000_mbr */
  123 
  124 int is_vista_mbr(FILE *fp)
  125 {
  126    #include "mbr_vista.h"
  127 
  128    return
  129       contains_data(fp, 0x0, mbr_vista_0x0, sizeof(mbr_vista_0x0)) &&
  130       is_br(fp);
  131 } /* is_vista_mbr */
  132 
  133 int is_win7_mbr(FILE *fp)
  134 {
  135    #include "mbr_win7.h"
  136 
  137    return
  138       contains_data(fp, 0x0, mbr_win7_0x0, sizeof(mbr_win7_0x0)) &&
  139       is_br(fp);
  140 } /* is_win7_mbr */
  141 
  142 int is_rufus_mbr(FILE *fp)
  143 {
  144    #include "mbr_rufus.h"
  145 
  146    return
  147       contains_data(fp, 0x0, mbr_rufus_0x0, sizeof(mbr_rufus_0x0)) &&
  148       is_br(fp);
  149 } /* is_rufus_mbr */
  150 
  151 int is_reactos_mbr(FILE *fp)
  152 {
  153    #include "mbr_reactos.h"
  154 
  155    return
  156       contains_data(fp, 0x0, mbr_reactos_0x0, sizeof(mbr_reactos_0x0)) &&
  157       is_br(fp);
  158 } /* is_reactos_mbr */
  159 
  160 int is_grub4dos_mbr(FILE *fp)
  161 {
  162    #include "mbr_grub.h"
  163 
  164    return
  165       contains_data(fp, 0x0, mbr_grub_0x0, sizeof(mbr_grub_0x0)) &&
  166       is_br(fp);
  167 } /* is_grub_mbr */
  168 
  169 int is_grub2_mbr(FILE *fp)
  170 {
  171    #include "mbr_grub2.h"
  172 
  173    return
  174       contains_data(fp, 0x0, mbr_grub2_0x0, sizeof(mbr_grub2_0x0)) &&
  175       is_br(fp);
  176 } /* is_grub2_mbr */
  177 
  178 int is_kolibrios_mbr(FILE *fp)
  179 {
  180    #include "mbr_kolibri.h"
  181 
  182    return
  183       contains_data(fp, 0x0, mbr_kolibri_0x0, sizeof(mbr_kolibri_0x0)) &&
  184       is_br(fp);
  185 } /* is_kolibri_mbr */
  186 
  187 int is_syslinux_mbr(FILE *fp)
  188 {
  189    #include "mbr_syslinux.h"
  190 
  191    return
  192       contains_data(fp, 0x0, mbr_syslinux_0x0, sizeof(mbr_syslinux_0x0)) &&
  193       is_br(fp);
  194 } /* is_syslinux_mbr */
  195 
  196 int is_syslinux_gpt_mbr(FILE *fp)
  197 {
  198    #include "mbr_gpt_syslinux.h"
  199 
  200    return
  201       contains_data(fp, 0x0, mbr_gpt_syslinux_0x0,
  202             sizeof(mbr_gpt_syslinux_0x0)) &&
  203       is_br(fp);
  204 } /* is_syslinux_gpt_mbr */
  205 
  206 int is_zero_mbr(FILE *fp)
  207 {
  208    #include "mbr_zero.h"
  209 
  210    return
  211       contains_data(fp, 0x0, mbr_zero_0x0, sizeof(mbr_zero_0x0));
  212     /* Don't bother to check 55AA signature */
  213 } /* is_zero_mbr */
  214 
  215 int is_zero_mbr_not_including_disk_signature_or_copy_protect(FILE *fp)
  216 {
  217    #include "mbr_zero.h"
  218 
  219    return
  220       contains_data(fp, 0x0, mbr_zero_0x0, 0x1b8);
  221 } /* is_zero_mbr_not_including_disk_signature_or_copy_protect */
  222 
  223 /* Handle nonstandard sector sizes (such as 4K) by writing
  224    the boot marker at every 512-2 bytes location */
  225 static int write_bootmark(FILE *fp)
  226 {
  227    unsigned char aucRef[] = {0x55, 0xAA};
  228    unsigned long pos = 0x1FE;
  229 
  230    for (pos = 0x1FE; pos < ulBytesPerSector; pos += 0x200) {
  231       if (!write_data(fp, pos, aucRef, sizeof(aucRef)))
  232             return 0;
  233    }
  234    return 1;
  235 }
  236 
  237 int write_dos_mbr(FILE *fp)
  238 {
  239    #include "mbr_dos.h"
  240 
  241    return
  242       write_data(fp, 0x0, mbr_dos_0x0, sizeof(mbr_dos_0x0)) &&
  243       write_bootmark(fp);
  244 } /* write_dos_mbr */
  245 
  246 int write_95b_mbr(FILE *fp)
  247 {
  248    #include "mbr_95b.h"
  249 
  250    return
  251       write_data(fp, 0x0,   mbr_95b_0x0, sizeof(mbr_95b_0x0)) &&
  252       write_data(fp, 0x0e0, mbr_95b_0x0e0, sizeof(mbr_95b_0x0e0)) &&
  253       write_bootmark(fp);
  254 } /* write_95b_mbr */
  255 
  256 int write_2000_mbr(FILE *fp)
  257 {
  258    #include "mbr_2000.h"
  259 
  260    return
  261       write_data(fp, 0x0, mbr_2000_0x0, sizeof(mbr_2000_0x0)) &&
  262       write_bootmark(fp);
  263 } /* write_2000_mbr */
  264 
  265 int write_vista_mbr(FILE *fp)
  266 {
  267    #include "mbr_vista.h"
  268 
  269    return
  270       write_data(fp, 0x0, mbr_vista_0x0, sizeof(mbr_vista_0x0)) &&
  271       write_bootmark(fp);
  272 } /* write_vista_mbr */
  273 
  274 int write_win7_mbr(FILE *fp)
  275 {
  276    #include "mbr_win7.h"
  277 
  278    return
  279       write_data(fp, 0x0, mbr_win7_0x0, sizeof(mbr_win7_0x0)) &&
  280       write_bootmark(fp);
  281 } /* write_win7_mbr */
  282 
  283 int write_rufus_mbr(FILE *fp)
  284 {
  285    #include "mbr_rufus.h"
  286 
  287    return
  288       write_data(fp, 0x0, mbr_rufus_0x0, sizeof(mbr_rufus_0x0)) &&
  289       write_bootmark(fp);
  290 } /* write_rufus_mbr */
  291 
  292 int write_reactos_mbr(FILE *fp)
  293 {
  294    #include "mbr_reactos.h"
  295 
  296    return
  297       write_data(fp, 0x0, mbr_reactos_0x0, sizeof(mbr_reactos_0x0)) &&
  298       write_bootmark(fp);
  299 } /* write_reactos_mbr */
  300 
  301 int write_kolibrios_mbr(FILE *fp)
  302 {
  303    #include "mbr_kolibri.h"
  304 
  305    return
  306       write_data(fp, 0x0, mbr_kolibri_0x0, sizeof(mbr_kolibri_0x0)) &&
  307       write_bootmark(fp);
  308 } /* write_kolibri_mbr */
  309 
  310 int write_syslinux_mbr(FILE *fp)
  311 {
  312    #include "mbr_syslinux.h"
  313 
  314    return
  315       write_data(fp, 0x0, mbr_syslinux_0x0, sizeof(mbr_syslinux_0x0)) &&
  316       write_bootmark(fp);
  317 } /* write_syslinux_mbr */
  318 
  319 int write_syslinux_gpt_mbr(FILE *fp)
  320 {
  321    #include "mbr_gpt_syslinux.h"
  322 
  323    return
  324       write_data(fp, 0x0, mbr_gpt_syslinux_0x0, sizeof(mbr_gpt_syslinux_0x0)) &&
  325       write_bootmark(fp);
  326 } /* write_syslinux_gpt_mbr */
  327 
  328 int write_grub4dos_mbr(FILE *fp)
  329 {
  330    #include "mbr_grub.h"
  331 
  332    return
  333       write_data(fp, 0x0, mbr_grub_0x0, sizeof(mbr_grub_0x0)) &&
  334       write_bootmark(fp);
  335 }
  336 
  337 int write_grub2_mbr(FILE *fp)
  338 {
  339    #include "mbr_grub2.h"
  340 
  341    return
  342       write_data(fp, 0x0, mbr_grub2_0x0, sizeof(mbr_grub2_0x0)) &&
  343       write_bootmark(fp);
  344 }
  345 
  346 int write_zero_mbr(FILE *fp)
  347 {
  348    #include "mbr_zero.h"
  349 
  350    return
  351       write_data(fp, 0x0, mbr_zero_0x0, sizeof(mbr_zero_0x0)) &&
  352       write_bootmark(fp);
  353 } /* write_zero_mbr */