+//\r
+// Any error level digitally larger than mNetDebugLevelMax \r
+// will be silently discarded.\r
+//\r
+UINTN mNetDebugLevelMax = NETDEBUG_LEVEL_ERROR;\r
+UINT32 mSyslogPacketSeq = 0xDEADBEEF;\r
+\r
+// \r
+// You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp \r
+// here to direct the syslog packets to the syslog deamon. The \r
+// default is broadcast to both the ethernet and IP. \r
+//\r
+UINT8 mSyslogDstMac[NET_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
+UINT32 mSyslogDstIp = 0xffffffff;\r
+UINT32 mSyslogSrcIp = 0;\r
+\r
+CHAR8 *\r
+mMonthName[] = {\r
+ "Jan",\r
+ "Feb",\r
+ "Mar",\r
+ "Apr",\r
+ "May",\r
+ "Jun",\r
+ "Jul",\r
+ "Aug",\r
+ "Sep",\r
+ "Oct",\r
+ "Nov",\r
+ "Dec"\r
+};\r
+\r
+/**\r
+ Locate the handles that support SNP, then open one of them \r
+ to send the syslog packets. The caller isn't required to close\r
+ the SNP after use because the SNP is opened by HandleProtocol.\r
+\r
+ @return The point to SNP if one is properly openned. Otherwise NULL\r
+\r
+**/\r
+EFI_SIMPLE_NETWORK_PROTOCOL *\r
+SyslogLocateSnp (\r
+ VOID\r
+ )\r
+{\r
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE *Handles;\r
+ UINTN HandleCount;\r
+ UINTN Index;\r
+\r
+ //\r
+ // Locate the handles which has SNP installed.\r
+ //\r
+ Handles = NULL;\r
+ Status = gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiSimpleNetworkProtocolGuid,\r
+ NULL,\r
+ &HandleCount,\r
+ &Handles\r
+ );\r
+\r
+ if (EFI_ERROR (Status) || (HandleCount == 0)) {\r
+ return NULL;\r
+ }\r
+ \r
+ //\r
+ // Try to open one of the ethernet SNP protocol to send packet\r
+ //\r
+ Snp = NULL;\r
+ \r
+ for (Index = 0; Index < HandleCount; Index++) {\r
+ Status = gBS->HandleProtocol (\r
+ Handles[Index],\r
+ &gEfiSimpleNetworkProtocolGuid,\r
+ (VOID **) &Snp\r
+ );\r
+\r
+ if ((Status == EFI_SUCCESS) && (Snp != NULL) && \r
+ (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) &&\r
+ (Snp->Mode->MaxPacketSize >= NET_SYSLOG_PACKET_LEN)) {\r
+ \r
+ break;\r
+ }\r
+\r
+ Snp = NULL;\r
+ }\r
+\r
+ FreePool (Handles);\r
+ return Snp;\r
+}\r
+\r
+/**\r
+ Transmit a syslog packet synchronously through SNP. The Packet\r
+ already has the ethernet header prepended. This function should \r
+ fill in the source MAC because it will try to locate a SNP each\r
+ time it is called to avoid the problem if SNP is unloaded.\r
+ This code snip is copied from MNP. \r
+\r
+ @param[in] Packet - The Syslog packet \r
+ @param[in] Length - The length of the packet\r
+\r
+ @retval EFI_DEVICE_ERROR - Failed to locate a usable SNP protocol\r
+ @retval EFI_TIMEOUT - Timeout happened to send the packet.\r
+ @retval EFI_SUCCESS - Packet is sent.\r
+ \r
+**/\r
+EFI_STATUS\r
+SyslogSendPacket (\r
+ IN CHAR8 *Packet,\r
+ IN UINT32 Length\r
+ )\r
+{\r
+ EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
+ ETHER_HEAD *Ether;\r
+ EFI_STATUS Status;\r
+ EFI_EVENT TimeoutEvent;\r
+ UINT8 *TxBuf;\r
+\r
+ Snp = SyslogLocateSnp ();\r
+\r
+ if (Snp == NULL) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ Ether = (ETHER_HEAD *) Packet;\r
+ CopyMem (Ether->SrcMac, Snp->Mode->CurrentAddress.Addr, NET_ETHER_ADDR_LEN);\r
+\r
+ //\r
+ // Start the timeout event.\r
+ //\r
+ Status = gBS->CreateEvent (\r
+ EVT_TIMER,\r
+ TPL_NOTIFY,\r
+ NULL,\r
+ NULL,\r
+ &TimeoutEvent\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ Status = gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto ON_EXIT;\r
+ }\r
+\r
+ for (;;) {\r
+ //\r
+ // Transmit the packet through SNP.\r
+ //\r
+ Status = Snp->Transmit (Snp, 0, Length, Packet, NULL, NULL, NULL);\r
+\r
+ if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {\r
+ Status = EFI_DEVICE_ERROR;\r
+ break;\r
+ }\r
+ \r
+ //\r
+ // If Status is EFI_SUCCESS, the packet is put in the transmit queue.\r
+ // if Status is EFI_NOT_READY, the transmit engine of the network\r
+ // interface is busy. Both need to sync SNP.\r
+ //\r
+ TxBuf = NULL;\r
+\r
+ do {\r
+ //\r
+ // Get the recycled transmit buffer status.\r
+ //\r
+ Snp->GetStatus (Snp, NULL, (VOID **) &TxBuf);\r
+\r
+ if (!EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) {\r
+ Status = EFI_TIMEOUT;\r
+ break;\r
+ }\r
+\r
+ } while (TxBuf == NULL);\r
+\r
+ if ((Status == EFI_SUCCESS) || (Status == EFI_TIMEOUT)) {\r
+ break;\r
+ }\r
+ \r
+ //\r
+ // Status is EFI_NOT_READY. Restart the timer event and\r
+ // call Snp->Transmit again.\r
+ //\r
+ gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT);\r
+ }\r
+\r
+ gBS->SetTimer (TimeoutEvent, TimerCancel, 0);\r
+\r
+ON_EXIT:\r
+ gBS->CloseEvent (TimeoutEvent);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Build a syslog packet, including the Ethernet/Ip/Udp headers \r
+ and user's message. \r
+ \r
+ @param[in] Level - Syslog servity level\r
+ @param[in] Module - The module that generates the log\r
+ @param[in] File - The file that contains the current log\r
+ @param[in] Line - The line of code in the File that contains the current log\r
+ @param[in] Message - The log message\r
+ @param[in] BufLen - The lenght of the Buf\r
+ @param[out] Buf - The buffer to put the packet data\r
+\r
+Returns:\r
+\r
+ The length of the syslog packet built.\r
+\r
+**/\r
+UINT32\r
+SyslogBuildPacket (\r
+ IN UINT32 Level,\r
+ IN UINT8 *Module,\r
+ IN UINT8 *File,\r
+ IN UINT32 Line,\r
+ IN UINT8 *Message,\r
+ IN UINT32 BufLen,\r
+ OUT CHAR8 *Buf \r
+ )\r
+{\r
+ ETHER_HEAD *Ether;\r
+ IP4_HEAD *Ip4;\r
+ EFI_UDP_HEADER *Udp4;\r
+ EFI_TIME Time;\r
+ UINT32 Pri;\r
+ UINT32 Len;\r
+\r
+ //\r
+ // Fill in the Ethernet header. Leave alone the source MAC. \r
+ // SyslogSendPacket will fill in the address for us.\r
+ //\r
+ Ether = (ETHER_HEAD *) Buf;\r
+ CopyMem (Ether->DstMac, mSyslogDstMac, NET_ETHER_ADDR_LEN);\r
+ ZeroMem (Ether->SrcMac, NET_ETHER_ADDR_LEN);\r
+\r
+ Ether->EtherType = HTONS (0x0800); // IPv4 protocol\r
+\r
+ Buf += sizeof (ETHER_HEAD);\r
+ BufLen -= sizeof (ETHER_HEAD);\r
+\r
+ //\r
+ // Fill in the IP header\r
+ //\r
+ Ip4 = (IP4_HEAD *) Buf;\r
+ Ip4->HeadLen = 5;\r
+ Ip4->Ver = 4;\r
+ Ip4->Tos = 0;\r
+ Ip4->TotalLen = 0;\r
+ Ip4->Id = (UINT16) mSyslogPacketSeq;\r
+ Ip4->Fragment = 0;\r
+ Ip4->Ttl = 16;\r
+ Ip4->Protocol = 0x11;\r
+ Ip4->Checksum = 0;\r
+ Ip4->Src = mSyslogSrcIp;\r
+ Ip4->Dst = mSyslogDstIp;\r
+\r
+ Buf += sizeof (IP4_HEAD);\r
+ BufLen -= sizeof (IP4_HEAD);\r
+\r
+ //\r
+ // Fill in the UDP header, Udp checksum is optional. Leave it zero.\r
+ //\r
+ Udp4 = (EFI_UDP_HEADER *) Buf;\r
+ Udp4->SrcPort = HTONS (514);\r
+ Udp4->DstPort = HTONS (514);\r
+ Udp4->Length = 0;\r
+ Udp4->Checksum = 0;\r
+\r
+ Buf += sizeof (EFI_UDP_HEADER);\r
+ BufLen -= sizeof (EFI_UDP_HEADER);\r
+\r
+ //\r
+ // Build the syslog message body with <PRI> Timestamp machine module Message\r
+ //\r
+ Pri = ((NET_SYSLOG_FACILITY & 31) << 3) | (Level & 7);\r
+ gRT->GetTime (&Time, NULL);\r
+\r
+ //\r
+ // Use %a to format the ASCII strings, %s to format UNICODE strings\r
+ //\r
+ Len = 0;\r
+ Len += (UINT32) AsciiSPrint (\r
+ Buf,\r
+ BufLen,\r
+ "<%d> %a %d %d:%d:%d ",\r
+ Pri,\r
+ mMonthName [Time.Month-1], \r
+ Time.Day,\r
+ Time.Hour,\r
+ Time.Minute,\r
+ Time.Second\r
+ );\r
+ Len--;\r
+\r
+ Len += (UINT32) AsciiSPrint (\r
+ Buf + Len, \r
+ BufLen - Len, \r
+ "Tiano %a: %a (Line: %d File: %a)", \r
+ Module,\r
+ Message,\r
+ Line,\r
+ File\r
+ );\r
+ Len--;\r
+\r
+ //\r
+ // OK, patch the IP length/checksum and UDP length fields.\r
+ //\r
+ Len += sizeof (EFI_UDP_HEADER);\r
+ Udp4->Length = HTONS ((UINT16) Len);\r
+\r
+ Len += sizeof (IP4_HEAD);\r
+ Ip4->TotalLen = HTONS ((UINT16) Len);\r
+ Ip4->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Ip4, sizeof (IP4_HEAD)));\r
+\r
+ return Len + sizeof (ETHER_HEAD);\r
+}\r
+\r
+/**\r
+ Allocate a buffer, then format the message to it. This is a \r
+ help function for the NET_DEBUG_XXX macros. The PrintArg of \r
+ these macros treats the variable length print parameters as a \r
+ single parameter, and pass it to the NetDebugASPrint. For\r
+ example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))\r
+ if extracted to: \r
+ \r
+ NetDebugOutput (\r
+ NETDEBUG_LEVEL_TRACE, \r
+ "Tcp", \r
+ __FILE__,\r
+ __LINE__,\r
+ NetDebugASPrint ("State transit to %a\n", Name) \r
+ ) \r
+ \r
+ @param Format The ASCII format string.\r
+ @param ... The variable length parameter whose format is determined \r
+ by the Format string.\r
+\r
+ @return The buffer containing the formatted message,\r
+ or NULL if failed to allocate memory.\r
+\r
+**/\r
+CHAR8 *\r
+NetDebugASPrint (\r
+ IN CHAR8 *Format,\r
+ ...\r
+ )\r
+{\r
+ VA_LIST Marker;\r
+ CHAR8 *Buf;\r
+\r
+ Buf = (CHAR8 *) AllocatePool (NET_DEBUG_MSG_LEN);\r
+\r
+ if (Buf == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ VA_START (Marker, Format);\r
+ AsciiVSPrint (Buf, NET_DEBUG_MSG_LEN, Format, Marker);\r
+ VA_END (Marker);\r
+\r
+ return Buf;\r
+}\r
+\r
+/**\r
+ Builds an UDP4 syslog packet and send it using SNP.\r
+\r
+ This function will locate a instance of SNP then send the message through it.\r
+ Because it isn't open the SNP BY_DRIVER, apply caution when using it.\r
+\r
+ @param Level The servity level of the message.\r
+ @param Module The Moudle that generates the log.\r
+ @param File The file that contains the log.\r
+ @param Line The exact line that contains the log.\r
+ @param Message The user message to log.\r
+\r
+ @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet\r
+ @retval EFI_SUCCESS The log is discard because that it is more verbose \r
+ than the mNetDebugLevelMax. Or, it has been sent out.\r
+**/ \r
+EFI_STATUS\r
+NetDebugOutput (\r
+ IN UINT32 Level, \r
+ IN UINT8 *Module,\r
+ IN UINT8 *File,\r
+ IN UINT32 Line,\r
+ IN UINT8 *Message\r
+ )\r
+{\r
+ CHAR8 *Packet;\r
+ UINT32 Len;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Check whether the message should be sent out\r
+ //\r
+ if (Message == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (Level > mNetDebugLevelMax) {\r
+ Status = EFI_SUCCESS;\r
+ goto ON_EXIT;\r
+ }\r
+ \r
+ //\r
+ // Allocate a maxium of 1024 bytes, the caller should ensure\r
+ // that the message plus the ethernet/ip/udp header is shorter\r
+ // than this\r
+ //\r
+ Packet = (CHAR8 *) AllocatePool (NET_SYSLOG_PACKET_LEN);\r
+\r
+ if (Packet == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto ON_EXIT;\r
+ }\r
+ \r
+ //\r
+ // Build the message: Ethernet header + IP header + Udp Header + user data\r
+ //\r
+ Len = SyslogBuildPacket (\r
+ Level,\r
+ Module,\r
+ File,\r
+ Line,\r
+ Message,\r
+ NET_SYSLOG_PACKET_LEN,\r
+ Packet\r
+ );\r
+\r
+ mSyslogPacketSeq++;\r
+ Status = SyslogSendPacket (Packet, Len);\r
+ FreePool (Packet);\r
+\r
+ON_EXIT:\r
+ FreePool (Message);\r
+ return Status;\r
+}\r