選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

pnpbios.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * 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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <realmode.h>
  23. #include <pnpbios.h>
  24. /** @file
  25. *
  26. * PnP BIOS
  27. *
  28. */
  29. /** PnP BIOS structure */
  30. struct pnp_bios {
  31. /** Signature
  32. *
  33. * Must be equal to @c PNP_BIOS_SIGNATURE
  34. */
  35. uint32_t signature;
  36. /** Version as BCD (e.g. 1.0 is 0x10) */
  37. uint8_t version;
  38. /** Length of this structure */
  39. uint8_t length;
  40. /** System capabilities */
  41. uint16_t control;
  42. /** Checksum */
  43. uint8_t checksum;
  44. } __attribute__ (( packed ));
  45. /** Signature for a PnP BIOS structure */
  46. #define PNP_BIOS_SIGNATURE \
  47. ( ( '$' << 0 ) + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )
  48. /**
  49. * Test address for PnP BIOS structure
  50. *
  51. * @v offset Offset within BIOS segment to test
  52. * @ret rc Return status code
  53. */
  54. static int is_pnp_bios ( unsigned int offset ) {
  55. union {
  56. struct pnp_bios pnp_bios;
  57. uint8_t bytes[256]; /* 256 is maximum length possible */
  58. } u;
  59. size_t len;
  60. unsigned int i;
  61. uint8_t sum = 0;
  62. /* Read start of header and verify signature */
  63. copy_from_real ( &u.pnp_bios, BIOS_SEG, offset, sizeof ( u.pnp_bios ));
  64. if ( u.pnp_bios.signature != PNP_BIOS_SIGNATURE )
  65. return -EINVAL;
  66. /* Read whole header and verify checksum */
  67. len = u.pnp_bios.length;
  68. copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
  69. for ( i = 0 ; i < len ; i++ ) {
  70. sum += u.bytes[i];
  71. }
  72. if ( sum != 0 )
  73. return -EINVAL;
  74. DBG ( "Found PnP BIOS at %04x:%04x\n", BIOS_SEG, offset );
  75. return 0;
  76. }
  77. /**
  78. * Locate Plug-and-Play BIOS
  79. *
  80. * @ret pnp_offset Offset of PnP BIOS structure within BIOS segment
  81. *
  82. * The PnP BIOS structure will be at BIOS_SEG:pnp_offset. If no PnP
  83. * BIOS is found, -1 is returned.
  84. */
  85. int find_pnp_bios ( void ) {
  86. static int pnp_offset = 0;
  87. if ( pnp_offset )
  88. return pnp_offset;
  89. for ( pnp_offset = 0 ; pnp_offset < 0x10000 ; pnp_offset += 0x10 ) {
  90. if ( is_pnp_bios ( pnp_offset ) == 0 )
  91. return pnp_offset;
  92. }
  93. pnp_offset = -1;
  94. return pnp_offset;
  95. }