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

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