ucmd_netloop.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* netloop: ring-3 command. PHY loopback self-test (no cable/peer needed). */
  2. #include "ucmd_lib.h"
  3. #define NET_FRAME_MAX 1518
  4. static unsigned char rx_buf[NET_FRAME_MAX];
  5. void _start(const char *arg)
  6. {
  7. unsigned char frame[64], mac[6];
  8. unsigned int i, deadline;
  9. int got = 0;
  10. (void)arg;
  11. if (u_syscall1(SYS_NET_MAC, (int)mac) != 0) {
  12. u_puts("netloop: no NIC found\n");
  13. u_exit(1);
  14. }
  15. if (u_syscall1(SYS_NET_LOOPBACK, 1) != 0) {
  16. u_puts("netloop: not supported by this NIC\n");
  17. u_exit(1);
  18. }
  19. for (i = 0; i < 6; i++)
  20. frame[i] = 0xFF; /* broadcast */
  21. for (i = 0; i < 6; i++)
  22. frame[6 + i] = mac[i];
  23. frame[12] = 0x88;
  24. frame[13] = 0xB6; /* local experimental */
  25. frame[14] = 'N'; frame[15] = 'E'; frame[16] = 'B';
  26. frame[17] = 'O'; frame[18] = 'S'; frame[19] = 'L';
  27. frame[20] = 'O'; frame[21] = 'O';
  28. for (i = 22; i < 64; i++)
  29. frame[i] = 0x00;
  30. u_puts("netloop: PHY loopback on, sending...\n");
  31. u_syscall0(SYS_NET_ACQUIRE);
  32. if (u_syscall2(SYS_NET_TX, (int)frame, sizeof(frame)) != 0) {
  33. u_puts("netloop: transmit failed\n");
  34. u_syscall0(SYS_NET_RELEASE);
  35. u_syscall1(SYS_NET_LOOPBACK, 0);
  36. u_exit(1);
  37. }
  38. deadline = u_ticks_ms() + 1000;
  39. while ((int)(u_ticks_ms() - deadline) < 0) {
  40. int len = u_syscall2(SYS_NET_RX, (int)rx_buf, NET_FRAME_MAX);
  41. if (len < 0)
  42. continue;
  43. if (len >= 22 &&
  44. rx_buf[12] == 0x88 && rx_buf[13] == 0xB6 &&
  45. rx_buf[14] == 'N' && rx_buf[15] == 'E' &&
  46. rx_buf[16] == 'B' && rx_buf[17] == 'O' &&
  47. rx_buf[18] == 'S') {
  48. got = 1;
  49. break;
  50. }
  51. }
  52. u_syscall0(SYS_NET_RELEASE);
  53. u_syscall1(SYS_NET_LOOPBACK, 0);
  54. u_puts(got ? "netloop: PASS (frame came back)\n"
  55. : "netloop: FAIL (no frame came back)\n");
  56. u_exit(got ? 0 : 1);
  57. }