You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pnpbios.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <realmode.h>
  24. #include <pnpbios.h>
  25. /** @file
  26. *
  27. * PnP BIOS
  28. *
  29. */
  30. /** PnP BIOS structure */
  31. struct pnp_bios {
  32. /** Signature
  33. *
  34. * Must be equal to @c PNP_BIOS_SIGNATURE
  35. */
  36. uint32_t signature;
  37. /** Version as BCD (e.g. 1.0 is 0x10) */
  38. uint8_t version;
  39. /** Length of this structure */
  40. uint8_t length;
  41. /** System capabilities */
  42. uint16_t control;
  43. /** Checksum */
  44. uint8_t checksum;
  45. } __attribute__ (( packed ));
  46. /** Signature for a PnP BIOS structure */
  47. #define PNP_BIOS_SIGNATURE \
  48. ( ( '$' << 0 ) + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )
  49. /**
  50. * Test address for PnP BIOS structure
  51. *
  52. * @v offset Offset within BIOS segment to test
  53. * @ret rc Return status code
  54. */
  55. static int is_pnp_bios ( unsigned int offset ) {
  56. union {
  57. struct pnp_bios pnp_bios;
  58. uint8_t bytes[256]; /* 256 is maximum length possible */
  59. } u;
  60. size_t len;
  61. unsigned int i;
  62. uint8_t sum = 0;
  63. /* Read start of header and verify signature */
  64. copy_from_real ( &u.pnp_bios, BIOS_SEG, offset, sizeof ( u.pnp_bios ));
  65. if ( u.pnp_bios.signature != PNP_BIOS_SIGNATURE )
  66. return -EINVAL;
  67. /* Read whole header and verify checksum */
  68. len = u.pnp_bios.length;
  69. copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
  70. for ( i = 0 ; i < len ; i++ ) {
  71. sum += u.bytes[i];
  72. }
  73. if ( sum != 0 )
  74. return -EINVAL;
  75. DBG ( "Found PnP BIOS at %04x:%04x\n", BIOS_SEG, offset );
  76. return 0;
  77. }
  78. /**
  79. * Locate Plug-and-Play BIOS
  80. *
  81. * @ret pnp_offset Offset of PnP BIOS structure within BIOS segment
  82. *
  83. * The PnP BIOS structure will be at BIOS_SEG:pnp_offset. If no PnP
  84. * BIOS is found, -1 is returned.
  85. */
  86. int find_pnp_bios ( void ) {
  87. static int pnp_offset = 0;
  88. if ( pnp_offset )
  89. return pnp_offset;
  90. for ( pnp_offset = 0 ; pnp_offset < 0x10000 ; pnp_offset += 0x10 ) {
  91. if ( is_pnp_bios ( pnp_offset ) == 0 )
  92. return pnp_offset;
  93. }
  94. pnp_offset = -1;
  95. return pnp_offset;
  96. }