ucmd_netmac.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* netmac: ring-3 diagnostic. Temporarily override the NIC's MAC address.
  2. *
  3. * Used to test whether a switch/repeater is bound to a particular MAC: set
  4. * the board to the ION's MAC (with the ION powered off), then run dhcp/ping.
  5. * The setting is RAM-only and disappears on reboot. */
  6. #include "ucmd_lib.h"
  7. static int hexval(char c)
  8. {
  9. if (c >= '0' && c <= '9')
  10. return c - '0';
  11. if (c >= 'a' && c <= 'f')
  12. return c - 'a' + 10;
  13. if (c >= 'A' && c <= 'F')
  14. return c - 'A' + 10;
  15. return -1;
  16. }
  17. static int parse_mac(const char *s, unsigned char mac[6])
  18. {
  19. unsigned int i;
  20. for (i = 0; i < 6; i++) {
  21. int hi = hexval(s[0]);
  22. int lo = hexval(s[1]);
  23. if (hi < 0 || lo < 0)
  24. return -1;
  25. mac[i] = (unsigned char)((hi << 4) | lo);
  26. s += 2;
  27. if (i != 5) {
  28. if (*s != ':' && *s != '-')
  29. return -1;
  30. s++;
  31. }
  32. }
  33. return *s == '\0' ? 0 : -1;
  34. }
  35. void _start(const char *arg)
  36. {
  37. struct sys_netinfo n;
  38. unsigned char old[6], mac[6];
  39. if (!arg || !arg[0]) {
  40. u_puts("netmac: usage netmac <xx:xx:xx:xx:xx:xx>\n");
  41. u_exit(1);
  42. }
  43. if (parse_mac(arg, mac) != 0) {
  44. u_puts("netmac: invalid MAC address\n");
  45. u_exit(1);
  46. }
  47. if (u_syscall1(SYS_NET_INFO, (int)&n) != 1) {
  48. u_puts("netmac: no NIC found\n");
  49. u_exit(1);
  50. }
  51. u_syscall1(SYS_NET_MAC, (int)old);
  52. if (u_syscall1(SYS_NET_MAC_SET, (int)mac) != 0) {
  53. u_puts("netmac: NIC busy or MAC override unsupported\n");
  54. u_exit(1);
  55. }
  56. u_puts("netmac: ");
  57. u_put_mac(old);
  58. u_puts(" -> ");
  59. u_put_mac(mac);
  60. u_puts("\nnetmac: RAM-only; reboot restores the factory MAC\n");
  61. u_syscall0(SYS_NET_PRINT_INFO);
  62. u_exit(0);
  63. }