2010/8/16 Diego <[email protected]>:
> Salvem camaradas, fiz um programinha em C pro amigo verificar se a a
> partição contém a assinatura de boot, bem como corrigi-lá se necessário.
> Lembrando que, para uma partição ser "bootavel" ela precisa conter a palavra
> 0xAA55 na posição 510 no primeiro registro do disco.
>
> Segue o código:
> Compilar com gcc -Wall bootsig.c -o bootsig
> Rodar como root
>
> /*
>  * bootsig.c
>  *
>  * Verifica se a assinatura de boot do disco esta ok
>  * Permite corrigir a assinatura do boot
>  * Use somente em partições que devem ser bootaveis!
>  *
>  * Autor: aveng3r / irc.freenode.net / #slackware-br
>  *
>  */
>
> #include <unistd.h>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <fcntl.h>
> #include <sys/types.h>
>
> static char *prog = "boot_sig";
>
> static void help(void)
> {
>        fprintf(stderr, "\nUso: [-u] %s <particao>\n\n"
>                        "exemplos:\n\t%s /dev/sda1   (Imprime a assinatura
> atual)\n"
>                        "\n\t%s -u /dev/sda1   (Regrava a assinatura de
> boot)\n\n", prog, prog, prog);
>        exit(1);
> }
>
> #define DIE(x) \
>        do { \
>                perror(x); \
>                exit(1); \
>        } while (0)
>
> int main(int argc, char **argv)
> {
>        char *part;
>        int fd;
>        unsigned short boot_sig = 0;
>        const unsigned short BOOT_SIGNATURE = 0xAA55;
>        int update = 0;
>
>        prog = *argv;
>
>        if (argc < 2) {
>                help();
>        }
>
>        if (strcmp(argv[1], "-u") == 0) {
>                if (argc < 3) {
>                        fprintf(stderr, "\nInforme a particao!\n");
>                        help();
>                }
>                part = argv[2];
>                update = 1;
>        } else {
>                part = argv[1];
>        }
>
>        if ((fd = open(part, O_RDWR)) < 0) {
>                DIE("open");
>        }
>        if (lseek(fd, 510, SEEK_SET) == (off_t)-1) {
>                DIE("lseek");
>        }
>        if (update) {
>                if (write(fd, (const void *)&BOOT_SIGNATURE, 2) != 2) {
>                        DIE("write");
>                }
>                printf("Assinatura de boot atualizada com sucesso\n");
>        } else {
>                if (read(fd, (void *) &boot_sig, 2) != 2) {
>                        DIE("read");
>                }
>                printf("Assinatura de boot atual = 0x%02X\n", boot_sig);
>        }
>        close(fd);
>
>        return 0;
> }
>
> Demonstração de uso:
>
> r...@dark:c# fdisk -l
>
> Disk /dev/sda: 80.0 GB, 80026361856 bytes
> 255 heads, 63 sectors/track, 9729 cylinders
> Units = cylinders of 16065 * 512 = 8225280 bytes
> Sector size (logical/physical): 512 bytes / 512 bytes
> I/O size (minimum/optimal): 512 bytes / 512 bytes
> Disk identifier: 0x13a313a3
>
>   Device Boot      Start         End      Blocks   Id  System
> /dev/sda1   *           1        4438    35648203+   7  HPFS/NTFS
> /dev/sda2   *        4439        4560      979965   83  Linux
> /dev/sda3            4561        7235    21486937+  83  Linux
> /dev/sda4            7236        9729    20033055    5  Extended
> /dev/sda5            7236        9423    17575078+  83  Linux
> /dev/sda6            9424        9729     2457913+  82  Linux swap
> r...@dark:c# gcc bootsig.c -o bootsig
> r...@dark:c# ./bootsig /dev/sda1
> Assinatura de boot atual = 0xAA55
> r...@dark:c# ./bootsig /dev/sda6
> Assinatura de boot atual = 0x00
> r...@dark:c# ./bootsig -u /dev/sda6
> Assinatura de boot atualizada com sucesso
> r...@dark:c# ./bootsig /dev/sda6
> Assinatura de boot atual = 0xAA55

Legal, mas dava pra fazer isso com o dd. :)

dd if=/dev/sda bs=512 count=1 | hexdump | grep "^00001f0"
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55

-- 
GUS-BR - Grupo de Usuários de Slackware Brasil
http://www.slackwarebrasil.org/
http://groups.google.com/group/slack-users-br

Antes de perguntar:
http://www.istf.com.br/perguntas/

Para sair da lista envie um e-mail para:
[email protected]

Responder a