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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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., 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 <string.h>
  25. #include <errno.h>
  26. #include <byteswap.h>
  27. #include <ipxe/dhcp.h>
  28. #include <ipxe/dhcpopts.h>
  29. #include <ipxe/settings.h>
  30. #include <ipxe/device.h>
  31. #include <ipxe/netdevice.h>
  32. #include <ipxe/init.h>
  33. /** @file
  34. *
  35. * Network device configuration settings
  36. *
  37. */
  38. /** Network device predefined settings */
  39. const struct setting mac_setting __setting ( SETTING_NETDEV, mac ) = {
  40. .name = "mac",
  41. .description = "MAC address",
  42. .type = &setting_type_hex,
  43. };
  44. const struct setting bustype_setting __setting ( SETTING_NETDEV, bustype ) = {
  45. .name = "bustype",
  46. .description = "Bus type",
  47. .type = &setting_type_string,
  48. };
  49. const struct setting busloc_setting __setting ( SETTING_NETDEV, busloc ) = {
  50. .name = "busloc",
  51. .description = "Bus location",
  52. .type = &setting_type_uint32,
  53. };
  54. const struct setting busid_setting __setting ( SETTING_NETDEV, busid ) = {
  55. .name = "busid",
  56. .description = "Bus ID",
  57. .type = &setting_type_hex,
  58. };
  59. const struct setting chip_setting __setting ( SETTING_NETDEV, chip ) = {
  60. .name = "chip",
  61. .description = "Chip",
  62. .type = &setting_type_string,
  63. };
  64. /**
  65. * Store MAC address setting
  66. *
  67. * @v netdev Network device
  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_store_mac ( struct net_device *netdev,
  73. const void *data, size_t len ) {
  74. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  75. /* Record new MAC address */
  76. if ( data ) {
  77. if ( len != netdev->ll_protocol->ll_addr_len )
  78. return -EINVAL;
  79. memcpy ( netdev->ll_addr, data, len );
  80. } else {
  81. /* Reset MAC address if clearing setting */
  82. ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
  83. }
  84. return 0;
  85. }
  86. /**
  87. * Fetch MAC address setting
  88. *
  89. * @v netdev Network device
  90. * @v data Buffer to fill with setting data
  91. * @v len Length of buffer
  92. * @ret len Length of setting data, or negative error
  93. */
  94. static int netdev_fetch_mac ( struct net_device *netdev, void *data,
  95. size_t len ) {
  96. if ( len > netdev->ll_protocol->ll_addr_len )
  97. len = netdev->ll_protocol->ll_addr_len;
  98. memcpy ( data, netdev->ll_addr, len );
  99. return netdev->ll_protocol->ll_addr_len;
  100. }
  101. /**
  102. * Fetch bus type setting
  103. *
  104. * @v netdev Network device
  105. * @v data Buffer to fill with setting data
  106. * @v len Length of buffer
  107. * @ret len Length of setting data, or negative error
  108. */
  109. static int netdev_fetch_bustype ( struct net_device *netdev, void *data,
  110. size_t len ) {
  111. static const char *bustypes[] = {
  112. [BUS_TYPE_PCI] = "PCI",
  113. [BUS_TYPE_ISAPNP] = "ISAPNP",
  114. [BUS_TYPE_EISA] = "EISA",
  115. [BUS_TYPE_MCA] = "MCA",
  116. [BUS_TYPE_ISA] = "ISA",
  117. [BUS_TYPE_TAP] = "TAP",
  118. };
  119. struct device_description *desc = &netdev->dev->desc;
  120. const char *bustype;
  121. assert ( desc->bus_type < ( sizeof ( bustypes ) /
  122. sizeof ( bustypes[0] ) ) );
  123. bustype = bustypes[desc->bus_type];
  124. assert ( bustype != NULL );
  125. strncpy ( data, bustype, len );
  126. return strlen ( bustype );
  127. }
  128. /**
  129. * Fetch bus location setting
  130. *
  131. * @v netdev Network device
  132. * @v data Buffer to fill with setting data
  133. * @v len Length of buffer
  134. * @ret len Length of setting data, or negative error
  135. */
  136. static int netdev_fetch_busloc ( struct net_device *netdev, void *data,
  137. size_t len ) {
  138. struct device_description *desc = &netdev->dev->desc;
  139. uint32_t busloc;
  140. busloc = cpu_to_be32 ( desc->location );
  141. if ( len > sizeof ( busloc ) )
  142. len = sizeof ( busloc );
  143. memcpy ( data, &busloc, len );
  144. return sizeof ( busloc );
  145. }
  146. /**
  147. * Fetch bus ID setting
  148. *
  149. * @v netdev Network device
  150. * @v data Buffer to fill with setting data
  151. * @v len Length of buffer
  152. * @ret len Length of setting data, or negative error
  153. */
  154. static int netdev_fetch_busid ( struct net_device *netdev, void *data,
  155. size_t len ) {
  156. struct device_description *desc = &netdev->dev->desc;
  157. struct dhcp_netdev_desc dhcp_desc;
  158. dhcp_desc.type = desc->bus_type;
  159. dhcp_desc.vendor = htons ( desc->vendor );
  160. dhcp_desc.device = htons ( desc->device );
  161. if ( len > sizeof ( dhcp_desc ) )
  162. len = sizeof ( dhcp_desc );
  163. memcpy ( data, &dhcp_desc, len );
  164. return sizeof ( dhcp_desc );
  165. }
  166. /**
  167. * Fetch chip setting
  168. *
  169. * @v netdev Network device
  170. * @v data Buffer to fill with setting data
  171. * @v len Length of buffer
  172. * @ret len Length of setting data, or negative error
  173. */
  174. static int netdev_fetch_chip ( struct net_device *netdev, void *data,
  175. size_t len ) {
  176. const char *chip = netdev->dev->driver_name;
  177. strncpy ( data, chip, len );
  178. return strlen ( chip );
  179. }
  180. /** A network device setting operation */
  181. struct netdev_setting_operation {
  182. /** Setting */
  183. const struct setting *setting;
  184. /** Store setting (or NULL if not supported)
  185. *
  186. * @v netdev Network device
  187. * @v data Setting data, or NULL to clear setting
  188. * @v len Length of setting data
  189. * @ret rc Return status code
  190. */
  191. int ( * store ) ( struct net_device *netdev, const void *data,
  192. size_t len );
  193. /** Fetch setting
  194. *
  195. * @v netdev Network device
  196. * @v data Buffer to fill with setting data
  197. * @v len Length of buffer
  198. * @ret len Length of setting data, or negative error
  199. */
  200. int ( * fetch ) ( struct net_device *netdev, void *data, size_t len );
  201. };
  202. /** Network device settings */
  203. static struct netdev_setting_operation netdev_setting_operations[] = {
  204. { &mac_setting, netdev_store_mac, netdev_fetch_mac },
  205. { &bustype_setting, NULL, netdev_fetch_bustype },
  206. { &busloc_setting, NULL, netdev_fetch_busloc },
  207. { &busid_setting, NULL, netdev_fetch_busid },
  208. { &chip_setting, NULL, netdev_fetch_chip },
  209. };
  210. /**
  211. * Store value of network device setting
  212. *
  213. * @v settings Settings block
  214. * @v setting Setting to store
  215. * @v data Setting data, or NULL to clear setting
  216. * @v len Length of setting data
  217. * @ret rc Return status code
  218. */
  219. static int netdev_store ( struct settings *settings,
  220. const struct setting *setting,
  221. const void *data, size_t len ) {
  222. struct net_device *netdev = container_of ( settings, struct net_device,
  223. settings.settings );
  224. struct netdev_setting_operation *op;
  225. unsigned int i;
  226. /* Handle network device-specific settings */
  227. for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
  228. sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
  229. op = &netdev_setting_operations[i];
  230. if ( setting_cmp ( setting, op->setting ) == 0 ) {
  231. if ( op->store ) {
  232. return op->store ( netdev, data, len );
  233. } else {
  234. return -ENOTSUP;
  235. }
  236. }
  237. }
  238. return generic_settings_store ( settings, setting, data, len );
  239. }
  240. /**
  241. * Fetch value of network device setting
  242. *
  243. * @v settings Settings block
  244. * @v setting Setting to fetch
  245. * @v data Buffer to fill with setting data
  246. * @v len Length of buffer
  247. * @ret len Length of setting data, or negative error
  248. */
  249. static int netdev_fetch ( struct settings *settings, struct setting *setting,
  250. void *data, size_t len ) {
  251. struct net_device *netdev = container_of ( settings, struct net_device,
  252. settings.settings );
  253. struct netdev_setting_operation *op;
  254. unsigned int i;
  255. /* Handle network device-specific settings */
  256. for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
  257. sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
  258. op = &netdev_setting_operations[i];
  259. if ( setting_cmp ( setting, op->setting ) == 0 )
  260. return op->fetch ( netdev, data, len );
  261. }
  262. return generic_settings_fetch ( settings, setting, data, len );
  263. }
  264. /**
  265. * Clear network device settings
  266. *
  267. * @v settings Settings block
  268. */
  269. static void netdev_clear ( struct settings *settings ) {
  270. generic_settings_clear ( settings );
  271. }
  272. /** Network device configuration settings operations */
  273. struct settings_operations netdev_settings_operations = {
  274. .store = netdev_store,
  275. .fetch = netdev_fetch,
  276. .clear = netdev_clear,
  277. };
  278. /**
  279. * Redirect "netX" settings block
  280. *
  281. * @v settings Settings block
  282. * @ret settings Underlying settings block
  283. */
  284. static struct settings * netdev_redirect ( struct settings *settings ) {
  285. struct net_device *netdev;
  286. /* Redirect to most recently opened network device */
  287. netdev = last_opened_netdev();
  288. if ( netdev ) {
  289. return netdev_settings ( netdev );
  290. } else {
  291. return settings;
  292. }
  293. }
  294. /** "netX" settings operations */
  295. static struct settings_operations netdev_redirect_settings_operations = {
  296. .redirect = netdev_redirect,
  297. };
  298. /** "netX" settings */
  299. static struct settings netdev_redirect_settings = {
  300. .refcnt = NULL,
  301. .siblings = LIST_HEAD_INIT ( netdev_redirect_settings.siblings ),
  302. .children = LIST_HEAD_INIT ( netdev_redirect_settings.children ),
  303. .op = &netdev_redirect_settings_operations,
  304. };
  305. /** Initialise "netX" settings */
  306. static void netdev_redirect_settings_init ( void ) {
  307. int rc;
  308. if ( ( rc = register_settings ( &netdev_redirect_settings, NULL,
  309. "netX" ) ) != 0 ) {
  310. DBG ( "Could not register netX settings: %s\n",
  311. strerror ( rc ) );
  312. return;
  313. }
  314. }
  315. /** "netX" settings initialiser */
  316. struct init_fn netdev_redirect_settings_init_fn __init_fn ( INIT_LATE ) = {
  317. .initialise = netdev_redirect_settings_init,
  318. };