fileXX.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/fileid.h>
  19. #include <grub/elfload.h>
  20. #include <grub/misc.h>
  21. #pragma GCC diagnostic ignored "-Wcast-align"
  22. int
  23. grub_file_check_netbsdXX (grub_elf_t elf)
  24. {
  25. Elf_Shdr *s, *s0;
  26. grub_size_t shnum = elf->ehdr.ehdrXX.e_shnum;
  27. grub_size_t shentsize = elf->ehdr.ehdrXX.e_shentsize;
  28. grub_size_t shsize = shnum * shentsize;
  29. grub_off_t stroff;
  30. if (!shnum || !shentsize)
  31. return 0;
  32. s0 = grub_malloc (shsize);
  33. if (!s0)
  34. return 0;
  35. if (grub_file_seek (elf->file, elf->ehdr.ehdrXX.e_shoff) == (grub_off_t) -1)
  36. goto fail;
  37. if (grub_file_read (elf->file, s0, shsize) != (grub_ssize_t) shsize)
  38. goto fail;
  39. s = (Elf_Shdr *) ((char *) s0 + elf->ehdr.ehdrXX.e_shstrndx * shentsize);
  40. stroff = s->sh_offset;
  41. for (s = s0; s < (Elf_Shdr *) ((char *) s0 + shnum * shentsize);
  42. s = (Elf_Shdr *) ((char *) s + shentsize))
  43. {
  44. char name[sizeof(".note.netbsd.ident")];
  45. grub_memset (name, 0, sizeof (name));
  46. if (grub_file_seek (elf->file, stroff + s->sh_name) == (grub_off_t) -1)
  47. goto fail;
  48. if (grub_file_read (elf->file, name, sizeof (name)) != (grub_ssize_t) sizeof (name))
  49. {
  50. if (grub_errno)
  51. goto fail;
  52. continue;
  53. }
  54. if (grub_memcmp (name, ".note.netbsd.ident",
  55. sizeof(".note.netbsd.ident")) != 0)
  56. continue;
  57. grub_free (s0);
  58. return 1;
  59. }
  60. fail:
  61. grub_free (s0);
  62. return 0;
  63. }