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.

netdev_settings.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2008 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 <string.h>
  20. #include <errno.h>
  21. #include <byteswap.h>
  22. #include <gpxe/dhcp.h>
  23. #include <gpxe/settings.h>
  24. #include <gpxe/device.h>
  25. #include <gpxe/netdevice.h>
  26. /** @file
  27. *
  28. * Network device configuration settings
  29. *
  30. */
  31. /** Network device named settings */
  32. struct setting mac_setting __setting = {
  33. .name = "mac",
  34. .description = "MAC address",
  35. .type = &setting_type_hex,
  36. };
  37. struct setting busid_setting __setting = {
  38. .name = "busid",
  39. .description = "Bus ID",
  40. .type = &setting_type_hex,
  41. };
  42. /**
  43. * Store value of network device setting
  44. *
  45. * @v settings Settings block
  46. * @v setting Setting to store
  47. * @v data Setting data, or NULL to clear setting
  48. * @v len Length of setting data
  49. * @ret rc Return status code
  50. */
  51. static int netdev_store ( struct settings *settings, struct setting *setting,
  52. const void *data, size_t len ) {
  53. struct net_device *netdev = container_of ( settings, struct net_device,
  54. settings.settings );
  55. if ( setting_cmp ( setting, &mac_setting ) == 0 ) {
  56. if ( len != netdev->ll_protocol->ll_addr_len )
  57. return -EINVAL;
  58. memcpy ( netdev->ll_addr, data, len );
  59. return 0;
  60. }
  61. return generic_settings_store ( settings, setting, data, len );
  62. }
  63. /**
  64. * Fetch value of network device setting
  65. *
  66. * @v settings Settings block
  67. * @v setting Setting to fetch
  68. * @v data Setting data, or NULL to clear setting
  69. * @v len Length of setting data
  70. * @ret rc Return status code
  71. */
  72. static int netdev_fetch ( struct settings *settings, struct setting *setting,
  73. void *data, size_t len ) {
  74. struct net_device *netdev = container_of ( settings, struct net_device,
  75. settings.settings );
  76. struct device_description *desc = &netdev->dev->desc;
  77. struct dhcp_netdev_desc dhcp_desc;
  78. if ( setting_cmp ( setting, &mac_setting ) == 0 ) {
  79. if ( len > netdev->ll_protocol->ll_addr_len )
  80. len = netdev->ll_protocol->ll_addr_len;
  81. memcpy ( data, netdev->ll_addr, len );
  82. return netdev->ll_protocol->ll_addr_len;
  83. }
  84. if ( setting_cmp ( setting, &busid_setting ) == 0 ) {
  85. dhcp_desc.type = desc->bus_type;
  86. dhcp_desc.vendor = htons ( desc->vendor );
  87. dhcp_desc.device = htons ( desc->device );
  88. if ( len > sizeof ( dhcp_desc ) )
  89. len = sizeof ( dhcp_desc );
  90. memcpy ( data, &dhcp_desc, len );
  91. return sizeof ( dhcp_desc );
  92. }
  93. return generic_settings_fetch ( settings, setting, data, len );
  94. }
  95. /**
  96. * Clear network device settings
  97. *
  98. * @v settings Settings block
  99. */
  100. static void netdev_clear ( struct settings *settings ) {
  101. generic_settings_clear ( settings );
  102. }
  103. /** Network device configuration settings operations */
  104. struct settings_operations netdev_settings_operations = {
  105. .store = netdev_store,
  106. .fetch = netdev_fetch,
  107. .clear = netdev_clear,
  108. };