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.9KB

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