]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kdp/kdp_udp.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / kdp / kdp_udp.c
index 1a84b2bfcd2e7a15c16efdcf949df82c31c70baf..c8636563a81c3ca88f2925b889ff4ec6d5358799 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -22,7 +22,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
@@ -66,7 +66,7 @@
 /* we just want the link status flags, so undef KERNEL_PRIVATE for this
  * header file. */
 #undef KERNEL_PRIVATE
-#include <net/if_media.h> 
+#include <net/if_media.h>
 #define KERNEL_PRIVATE
 
 #include <string.h>
@@ -85,7 +85,7 @@ extern int      inet_aton(const char *, struct kdp_in_addr *); /* in libkern */
 extern char    *inet_ntoa_r(struct kdp_in_addr ina, char *buf,
     size_t buflen); /* in libkern */
 
-#define DO_ALIGN       1             /* align all packet data accesses */
+#define DO_ALIGN        1             /* align all packet data accesses */
 #define KDP_SERIAL_IPADDR  0xABADBABE /* IP address used for serial KDP */
 #define LINK_UP_STATUS     (IFM_AVALID | IFM_ACTIVE)
 
@@ -100,41 +100,41 @@ static u_short ip_id;                          /* ip packet ctr, for ids */
  * UDP protocol implementation.
  * Per RFC 768, August, 1980.
  */
-#define UDP_TTL        60 /* deflt time to live for UDP packets */
+#define UDP_TTL 60 /* deflt time to live for UDP packets */
 static int udp_ttl = UDP_TTL;
-static unsigned char   exception_seq;
+static unsigned char    exception_seq;
 
 struct kdp_ipovly {
-        uint32_t ih_next, ih_prev;     /* for protocol sequence q's */
-        u_char  ih_x1;                 /* (unused) */
-        u_char  ih_pr;                 /* protocol */
-        short   ih_len;                        /* protocol length */
-        struct  kdp_in_addr ih_src;    /* source internet address */
-        struct  kdp_in_addr ih_dst;    /* destination internet address */
+       uint32_t ih_next, ih_prev;      /* for protocol sequence q's */
+       u_char  ih_x1;                  /* (unused) */
+       u_char  ih_pr;                  /* protocol */
+       short   ih_len;                 /* protocol length */
+       struct  kdp_in_addr ih_src;     /* source internet address */
+       struct  kdp_in_addr ih_dst;     /* destination internet address */
 };
 
 struct kdp_udphdr {
-       u_short uh_sport;               /* source port */
-       u_short uh_dport;               /* destination port */
-       short   uh_ulen;                /* udp length */
-       u_short uh_sum;                 /* udp checksum */
+       u_short uh_sport;               /* source port */
+       u_short uh_dport;               /* destination port */
+       short   uh_ulen;                /* udp length */
+       u_short uh_sum;                 /* udp checksum */
 };
 
 struct  kdp_udpiphdr {
-        struct  kdp_ipovly ui_i;       /* overlaid ip structure */
-        struct  kdp_udphdr ui_u;       /* udp header */
+       struct  kdp_ipovly ui_i;        /* overlaid ip structure */
+       struct  kdp_udphdr ui_u;        /* udp header */
 };
-#define        ui_next         ui_i.ih_next
-#define        ui_prev         ui_i.ih_prev
-#define        ui_x1           ui_i.ih_x1
-#define        ui_pr           ui_i.ih_pr
-#define        ui_len          ui_i.ih_len
-#define        ui_src          ui_i.ih_src
-#define        ui_dst          ui_i.ih_dst
-#define        ui_sport        ui_u.uh_sport
-#define        ui_dport        ui_u.uh_dport
-#define        ui_ulen         ui_u.uh_ulen
-#define        ui_sum          ui_u.uh_sum
+#define ui_next         ui_i.ih_next
+#define ui_prev         ui_i.ih_prev
+#define ui_x1           ui_i.ih_x1
+#define ui_pr           ui_i.ih_pr
+#define ui_len          ui_i.ih_len
+#define ui_src          ui_i.ih_src
+#define ui_dst          ui_i.ih_dst
+#define ui_sport        ui_u.uh_sport
+#define ui_dport        ui_u.uh_dport
+#define ui_ulen         ui_u.uh_ulen
+#define ui_sum          ui_u.uh_sum
 
 struct kdp_ip {
        union {
@@ -142,38 +142,38 @@ struct kdp_ip {
                struct {
                        unsigned int
 #ifdef __LITTLE_ENDIAN__
-                       ip_xhl:4,       /* header length */
-                       ip_xv:4,        /* version */
-                       ip_xtos:8,      /* type of service */
-                       ip_xlen:16;     /* total length */
+                           ip_xhl:4,   /* header length */
+                           ip_xv:4,    /* version */
+                           ip_xtos:8,  /* type of service */
+                           ip_xlen:16; /* total length */
 #endif
 #ifdef __BIG_ENDIAN__
-                       ip_xv:4,        /* version */
-                       ip_xhl:4,       /* header length */
-                       ip_xtos:8,      /* type of service */
-                       ip_xlen:16;     /* total length */
+                       ip_xv:4,        /* version */
+                       ip_xhl:4,       /* header length */
+                       ip_xtos:8,      /* type of service */
+                       ip_xlen:16;     /* total length */
 #endif
                } ip_x;
        } ip_vhltl;
-        u_short ip_id;                 /* identification */
-        short   ip_off;                        /* fragment offset field */
-#define        IP_DF 0x4000                    /* dont fragment flag */
-#define        IP_MF 0x2000                    /* more fragments flag */
-#define        IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
-        u_char  ip_ttl;                        /* time to live */
-        u_char  ip_p;                  /* protocol */
-        u_short ip_sum;                        /* checksum */
-        struct  kdp_in_addr ip_src,ip_dst;  /* source and dest address */
+       u_short ip_id;                  /* identification */
+       short   ip_off;                 /* fragment offset field */
+#define IP_DF 0x4000                    /* dont fragment flag */
+#define IP_MF 0x2000                    /* more fragments flag */
+#define IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
+       u_char  ip_ttl;                 /* time to live */
+       u_char  ip_p;                   /* protocol */
+       u_short ip_sum;                 /* checksum */
+       struct  kdp_in_addr ip_src, ip_dst;  /* source and dest address */
 };
-#define        ip_v            ip_vhltl.ip_x.ip_xv
-#define        ip_hl           ip_vhltl.ip_x.ip_xhl
-#define        ip_tos          ip_vhltl.ip_x.ip_xtos
-#define        ip_len          ip_vhltl.ip_x.ip_xlen
+#define ip_v            ip_vhltl.ip_x.ip_xv
+#define ip_hl           ip_vhltl.ip_x.ip_xhl
+#define ip_tos          ip_vhltl.ip_x.ip_xtos
+#define ip_len          ip_vhltl.ip_x.ip_xlen
 
-#define        IPPROTO_UDP     17
-#define        IPVERSION       4
+#define IPPROTO_UDP     17
+#define IPVERSION       4
 
-#define        ETHERTYPE_IP    0x0800  /* IP protocol */
+#define ETHERTYPE_IP    0x0800  /* IP protocol */
 
 /*
  * Ethernet Address Resolution Protocol.
@@ -183,70 +183,70 @@ struct kdp_ip {
  * RFC 826.
  */
 
-#define        ETHERTYPE_ARP   0x0806  /* Addr. resolution protocol */
+#define ETHERTYPE_ARP   0x0806  /* Addr. resolution protocol */
 
 struct  kdp_arphdr {
        u_short ar_hrd;         /* format of hardware address */
-#define        ARPHRD_ETHER    1       /* ethernet hardware format */
-#define        ARPHRD_FRELAY   15      /* frame relay hardware format */
+#define ARPHRD_ETHER    1       /* ethernet hardware format */
+#define ARPHRD_FRELAY   15      /* frame relay hardware format */
        u_short ar_pro;         /* format of protocol address */
        u_char  ar_hln;         /* length of hardware address */
        u_char  ar_pln;         /* length of protocol address */
        u_short ar_op;          /* one of: */
-#define        ARPOP_REQUEST   1       /* request to resolve address */
-#define        ARPOP_REPLY     2       /* response to previous request */
-#define        ARPOP_REVREQUEST 3      /* request protocol address given hardware */
-#define        ARPOP_REVREPLY  4       /* response giving protocol address */
-#define        ARPOP_INVREQUEST 8      /* request to identify peer */
-#define        ARPOP_INVREPLY  9       /* response identifying peer */
+#define ARPOP_REQUEST   1       /* request to resolve address */
+#define ARPOP_REPLY     2       /* response to previous request */
+#define ARPOP_REVREQUEST 3      /* request protocol address given hardware */
+#define ARPOP_REVREPLY  4       /* response giving protocol address */
+#define ARPOP_INVREQUEST 8      /* request to identify peer */
+#define ARPOP_INVREPLY  9       /* response identifying peer */
 };
 
 struct  kdp_ether_arp {
-       struct  kdp_arphdr ea_hdr;              /* fixed-size header */
+       struct  kdp_arphdr ea_hdr;              /* fixed-size header */
        u_char  arp_sha[ETHER_ADDR_LEN];        /* sender hardware address */
-       u_char  arp_spa[4];                     /* sender protocol address */
+       u_char  arp_spa[4];                     /* sender protocol address */
        u_char  arp_tha[ETHER_ADDR_LEN];        /* target hardware address */
-       u_char  arp_tpa[4];                     /* target protocol address */
+       u_char  arp_tpa[4];                     /* target protocol address */
 };
-#define        arp_hrd ea_hdr.ar_hrd
-#define        arp_pro ea_hdr.ar_pro
-#define        arp_hln ea_hdr.ar_hln
-#define        arp_pln ea_hdr.ar_pln
-#define        arp_op  ea_hdr.ar_op
+#define arp_hrd ea_hdr.ar_hrd
+#define arp_pro ea_hdr.ar_pro
+#define arp_hln ea_hdr.ar_hln
+#define arp_pln ea_hdr.ar_pln
+#define arp_op  ea_hdr.ar_op
 
-#define        ETHERMTU        1500
-#define        ETHERHDRSIZE    14
-#define        ETHERCRC        4
-#define        KDP_MAXPACKET   (ETHERHDRSIZE + ETHERMTU + ETHERCRC)
+#define ETHERMTU        1500
+#define ETHERHDRSIZE    14
+#define ETHERCRC        4
+#define KDP_MAXPACKET   (ETHERHDRSIZE + ETHERMTU + ETHERCRC)
 
 static struct {
-       unsigned char   data[KDP_MAXPACKET];
-       unsigned int    off, len;
-       boolean_t               input;
+       unsigned char   data[KDP_MAXPACKET];
+       unsigned int    off, len;
+       boolean_t               input;
 } pkt, saved_reply;
 
 struct kdp_manual_pkt manual_pkt;
 
 struct {
        struct {
-               struct kdp_in_addr      in;
-               struct kdp_ether_addr   ea;
+               struct kdp_in_addr      in;
+               struct kdp_ether_addr   ea;
        } loc;
        struct {
-               struct kdp_in_addr      in;
-               struct kdp_ether_addr   ea;
+               struct kdp_in_addr      in;
+               struct kdp_ether_addr   ea;
        } rmt;
 } adr;
 
 static const char
 *exception_message[] = {
        "Unknown",
-       "Memory access",        /* EXC_BAD_ACCESS */
-       "Failed instruction",   /* EXC_BAD_INSTRUCTION */
-       "Arithmetic",           /* EXC_ARITHMETIC */
-       "Emulation",            /* EXC_EMULATION */
-       "Software",             /* EXC_SOFTWARE */
-       "Breakpoint"            /* EXC_BREAKPOINT */
+       "Memory access",        /* EXC_BAD_ACCESS */
+       "Failed instruction",   /* EXC_BAD_INSTRUCTION */
+       "Arithmetic",           /* EXC_ARITHMETIC */
+       "Emulation",            /* EXC_EMULATION */
+       "Software",             /* EXC_SOFTWARE */
+       "Breakpoint"            /* EXC_BREAKPOINT */
 };
 
 volatile int kdp_flag = 0;
@@ -270,7 +270,7 @@ static void *kdp_current_ifp;
 
 static void kdp_handler( void *);
 
-static uint32_t panic_server_ip = 0; 
+static uint32_t panic_server_ip = 0;
 static uint32_t parsed_router_ip = 0;
 static uint32_t router_ip = 0;
 static uint32_t target_ip = 0;
@@ -284,10 +284,10 @@ static unsigned int panicd_port = CORE_REMOTE_PORT;
 
 static struct kdp_ether_addr etherbroadcastaddr = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
 
-static struct kdp_ether_addr router_mac = {{0, 0, 0 , 0, 0, 0}};
-static struct kdp_ether_addr destination_mac = {{0, 0, 0 , 0, 0, 0}};
-static struct kdp_ether_addr temp_mac = {{0, 0, 0 , 0, 0, 0}};
-static struct kdp_ether_addr current_resolved_MAC = {{0, 0, 0 , 0, 0, 0}};
+static struct kdp_ether_addr router_mac = {{0, 0, 0, 0, 0, 0}};
+static struct kdp_ether_addr destination_mac = {{0, 0, 0, 0, 0, 0}};
+static struct kdp_ether_addr temp_mac = {{0, 0, 0, 0, 0, 0}};
+static struct kdp_ether_addr current_resolved_MAC = {{0, 0, 0, 0, 0, 0}};
 
 static boolean_t flag_panic_dump_in_progress = FALSE;
 static boolean_t flag_router_mac_initialized = FALSE;
@@ -313,26 +313,26 @@ __private_extern__ volatile unsigned int flag_kdp_trigger_reboot = 0;
 
 extern unsigned int disableConsoleOutput;
 
-extern void            kdp_call(void);
+extern void             kdp_call(void);
 
-void * kdp_get_interface(void);
+void *  kdp_get_interface(void);
 void    kdp_set_gateway_mac(void *gatewaymac);
-void   kdp_set_ip_and_mac_addresses(struct kdp_in_addr *ipaddr, struct kdp_ether_addr *);
-void   kdp_set_interface(void *interface, const struct kdp_ether_addr *macaddr);
+void    kdp_set_ip_and_mac_addresses(struct kdp_in_addr *ipaddr, struct kdp_ether_addr *);
+void    kdp_set_interface(void *interface, const struct kdp_ether_addr *macaddr);
 
-void                   kdp_disable_arp(void);
-static void            kdp_arp_reply(struct kdp_ether_arp *);
-static void            kdp_process_arp_reply(struct kdp_ether_arp *);
-static boolean_t       kdp_arp_resolve(uint32_t, struct kdp_ether_addr *);
+void                    kdp_disable_arp(void);
+static void             kdp_arp_reply(struct kdp_ether_arp *);
+static void             kdp_process_arp_reply(struct kdp_ether_arp *);
+static boolean_t        kdp_arp_resolve(uint32_t, struct kdp_ether_addr *);
 
-static volatile unsigned       kdp_reentry_deadline;
+static volatile unsigned        kdp_reentry_deadline;
 
 static uint32_t kdp_crashdump_feature_mask = KDP_FEATURE_LARGE_CRASHDUMPS | KDP_FEATURE_LARGE_PKT_SIZE;
 uint32_t kdp_feature_large_crashdumps, kdp_feature_large_pkt_size;
 
 char kdp_kernelversion_string[256];
 
-static boolean_t       gKDPDebug = FALSE;
+static boolean_t        gKDPDebug = FALSE;
 
 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
 
@@ -343,15 +343,17 @@ uint64_t kdp_max_superblock_dump_time = 0;
 uint64_t kdp_superblock_dump_time = 0;
 uint64_t kdp_superblock_dump_start_time = 0;
 static thread_call_t
-kdp_timer_call;
+    kdp_timer_call;
 
 static void
-kdp_ml_enter_debugger_wrapper(__unused void *param0, __unused void *param1) {
+kdp_ml_enter_debugger_wrapper(__unused void *param0, __unused void *param1)
+{
        kdp_ml_enter_debugger();
 }
 
 static void
-kdp_timer_callout_init(void) {
+kdp_timer_callout_init(void)
+{
        kdp_timer_call = thread_call_allocate(kdp_ml_enter_debugger_wrapper, NULL);
 }
 
@@ -362,12 +364,14 @@ wait_for_link(void)
 {
        static int first = 0;
 
-       if (!kdp_en_linkstatus)
+       if (!kdp_en_linkstatus) {
                return;
+       }
 
        while (((*kdp_en_linkstatus)() & LINK_UP_STATUS) != LINK_UP_STATUS) {
-               if (first)
+               if (first) {
                        continue;
+               }
 
                first = 1;
                printf("Waiting for link to become available.\n");
@@ -408,86 +412,102 @@ kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
 
 void
 kdp_register_send_receive(
-       kdp_send_t      send, 
-       kdp_receive_t   receive)
+       kdp_send_t      send,
+       kdp_receive_t   receive)
 {
-       unsigned int    debug = 0;
+       unsigned int    debug = 0;
 
-       PE_parse_boot_argn("debug", &debug, sizeof (debug));
+       PE_parse_boot_argn("debug", &debug, sizeof(debug));
 
 #if defined(__arm__) || defined(__arm64__)
        {
                uint32_t debug_flags;
 
-               if (!PE_i_can_has_debugger(&debug_flags))
+               if (!PE_i_can_has_debugger(&debug_flags)) {
                        debug = 0;
+               }
        }
 #endif
 
-       if (!debug)
+       if (!debug) {
                return;
+       }
 
        kdp_en_send_pkt   = send;
        kdp_en_recv_pkt   = receive;
 
-       if (debug & DB_KDP_BP_DIS)
-               kdp_flag |= KDP_BP_DIS;   
-       if (debug & DB_KDP_GETC_ENA)
-               kdp_flag |= KDP_GETC_ENA;   
-       if (debug & DB_ARP)
+       if (debug & DB_KDP_BP_DIS) {
+               kdp_flag |= KDP_BP_DIS;
+       }
+       if (debug & DB_KDP_GETC_ENA) {
+               kdp_flag |= KDP_GETC_ENA;
+       }
+       if (debug & DB_ARP) {
                kdp_flag |= KDP_ARP;
+       }
 
-       if (debug & DB_KERN_DUMP_ON_PANIC)
+       if (debug & DB_KERN_DUMP_ON_PANIC) {
                kdp_flag |= KDP_PANIC_DUMP_ENABLED;
-       if (debug & DB_KERN_DUMP_ON_NMI)
+       }
+       if (debug & DB_KERN_DUMP_ON_NMI) {
                kdp_flag |= PANIC_CORE_ON_NMI;
+       }
 
-       if (debug & DB_DBG_POST_CORE)
+       if (debug & DB_DBG_POST_CORE) {
                kdp_flag |= DBG_POST_CORE;
+       }
 
-       if (debug & DB_PANICLOG_DUMP)
+       if (debug & DB_PANICLOG_DUMP) {
                kdp_flag |= PANIC_LOG_DUMP;
+       }
 
        kdp_corezip_disabled = (0 != (debug & DB_DISABLE_GZIP_CORE));
 
-       if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str, sizeof (panicd_ip_str)))
+       if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str, sizeof(panicd_ip_str))) {
                panicd_specified = TRUE;
+       }
 
-       if ((debug & DB_REBOOT_POST_CORE) && (panicd_specified == TRUE))
+       if ((debug & DB_REBOOT_POST_CORE) && (panicd_specified == TRUE)) {
                kdp_flag |= REBOOT_POST_CORE;
+       }
 
-       if (PE_parse_boot_argn("_router_ip", router_ip_str, sizeof (router_ip_str)))
+       if (PE_parse_boot_argn("_router_ip", router_ip_str, sizeof(router_ip_str))) {
                router_specified = TRUE;
+       }
 
-       if (!PE_parse_boot_argn("panicd_port", &panicd_port, sizeof (panicd_port)))
+       if (!PE_parse_boot_argn("panicd_port", &panicd_port, sizeof(panicd_port))) {
                panicd_port = CORE_REMOTE_PORT;
+       }
 
-       if (PE_parse_boot_argn("_panicd_corename", &corename_str, sizeof (corename_str)))
+       if (PE_parse_boot_argn("_panicd_corename", &corename_str, sizeof(corename_str))) {
                corename_specified = TRUE;
+       }
 
        kdp_flag |= KDP_READY;
 
        current_debugger = KDP_CUR_DB;
        if ((kdp_current_ip_address != 0) && halt_in_debugger) {
-               kdp_call(); 
-               halt_in_debugger=0;
+               kdp_call();
+               halt_in_debugger = 0;
        }
 }
 
 void
 kdp_unregister_send_receive(
-       __unused kdp_send_t     send, 
-       __unused kdp_receive_t  receive)
+       __unused kdp_send_t     send,
+       __unused kdp_receive_t  receive)
 {
-       if (current_debugger == KDP_CUR_DB)
+       if (current_debugger == KDP_CUR_DB) {
                current_debugger = NO_CUR_DB;
+       }
        kdp_flag &= ~KDP_READY;
        kdp_en_send_pkt   = NULL;
        kdp_en_recv_pkt   = NULL;
 }
 
 static void
-kdp_schedule_debugger_reentry(unsigned interval) {
+kdp_schedule_debugger_reentry(unsigned interval)
+{
        uint64_t deadline;;
 
        clock_interval_to_deadline(interval, 1000 * 1000, &deadline);
@@ -496,53 +516,54 @@ kdp_schedule_debugger_reentry(unsigned interval) {
 
 static void
 enaddr_copy(
-       void    *src,
-       void    *dst
-)
+       void    *src,
+       void    *dst
+       )
 {
-       bcopy((char *)src, (char *)dst, sizeof (struct kdp_ether_addr));
+       bcopy((char *)src, (char *)dst, sizeof(struct kdp_ether_addr));
 }
 
 static unsigned short
 ip_sum(
-       unsigned char   *c,
-       unsigned int    hlen
+       unsigned char   *c,
+       unsigned int    hlen
        )
 {
-       unsigned int    high, low, sum;
-    
+       unsigned int    high, low, sum;
+
        high = low = 0;
        while (hlen-- > 0) {
                low += c[1] + c[3];
                high += c[0] + c[2];
-       
-               c += sizeof (int);
+
+               c += sizeof(int);
        }
-    
+
        sum = (high << 8) + low;
        sum = (sum >> 16) + (sum & 65535);
-    
-       return (sum > 65535 ? sum - 65535 : sum);
+
+       return sum > 65535 ? sum - 65535 : sum;
 }
 
 static void
 kdp_reply(
-          unsigned short               reply_port,
-          const boolean_t         sideband
-          )
+       unsigned short                reply_port,
+       const boolean_t         sideband
+       )
 {
-       struct kdp_udpiphdr     aligned_ui, *ui = &aligned_ui;
-       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
-       struct kdp_in_addr      tmp_ipaddr;
-       struct kdp_ether_addr   tmp_enaddr;
-       struct kdp_ether_header *eh = NULL;
-    
-       if (!pkt.input)
+       struct kdp_udpiphdr     aligned_ui, *ui = &aligned_ui;
+       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
+       struct kdp_in_addr      tmp_ipaddr;
+       struct kdp_ether_addr   tmp_enaddr;
+       struct kdp_ether_header *eh = NULL;
+
+       if (!pkt.input) {
                kdp_panic("kdp_reply");
-       
-       pkt.off -= (unsigned int)sizeof (struct kdp_udpiphdr);
+       }
+
+       pkt.off -= (unsigned int)sizeof(struct kdp_udpiphdr);
 
-#if DO_ALIGN    
+#if DO_ALIGN
        bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
 #else
        ui = (struct kdp_udpiphdr *)&pkt.data[pkt.off];
@@ -550,7 +571,7 @@ kdp_reply(
        ui->ui_next = ui->ui_prev = 0;
        ui->ui_x1 = 0;
        ui->ui_pr = IPPROTO_UDP;
-       ui->ui_len = htons((u_short)pkt.len + sizeof (struct kdp_udphdr));
+       ui->ui_len = htons((u_short)pkt.len + sizeof(struct kdp_udphdr));
        tmp_ipaddr = ui->ui_src;
        ui->ui_src = ui->ui_dst;
        ui->ui_dst = tmp_ipaddr;
@@ -564,54 +585,57 @@ kdp_reply(
 #else
        ip = (struct kdp_ip *)&pkt.data[pkt.off];
 #endif
-       ip->ip_len = htons(sizeof (struct kdp_udpiphdr) + pkt.len);
+       ip->ip_len = htons(sizeof(struct kdp_udpiphdr) + pkt.len);
        ip->ip_v = IPVERSION;
        ip->ip_id = htons(ip_id++);
-       ip->ip_hl = sizeof (struct kdp_ip) >> 2;
+       ip->ip_hl = sizeof(struct kdp_ip) >> 2;
        ip->ip_ttl = udp_ttl;
        ip->ip_sum = 0;
        ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
 #if DO_ALIGN
        bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
 #endif
-    
-       pkt.len += (unsigned int)sizeof (struct kdp_udpiphdr);
-    
-       pkt.off -= (unsigned int)sizeof (struct kdp_ether_header);
-    
+
+       pkt.len += (unsigned int)sizeof(struct kdp_udpiphdr);
+
+       pkt.off -= (unsigned int)sizeof(struct kdp_ether_header);
+
        eh = (struct kdp_ether_header *)&pkt.data[pkt.off];
        enaddr_copy(eh->ether_shost, &tmp_enaddr);
        enaddr_copy(eh->ether_dhost, eh->ether_shost);
        enaddr_copy(&tmp_enaddr, eh->ether_dhost);
        eh->ether_type = htons(ETHERTYPE_IP);
-    
-       pkt.len += (unsigned int)sizeof (struct kdp_ether_header);
-    
+
+       pkt.len += (unsigned int)sizeof(struct kdp_ether_header);
+
        // save reply for possible retransmission
        assert(pkt.len <= KDP_MAXPACKET);
-       if (!sideband)
+       if (!sideband) {
                bcopy((char *)&pkt, (char *)&saved_reply, sizeof(saved_reply));
+       }
 
        kdp_send_data(&pkt.data[pkt.off], pkt.len);
 
        // increment expected sequence number
-       if (!sideband) 
+       if (!sideband) {
                exception_seq++;
+       }
 }
 
 static void
 kdp_send(
-    unsigned short             remote_port
-)
+       unsigned short              remote_port
+       )
 {
-       struct kdp_udpiphdr             aligned_ui, *ui = &aligned_ui;
-       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
-       struct kdp_ether_header *eh;
+       struct kdp_udpiphdr             aligned_ui, *ui = &aligned_ui;
+       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
+       struct kdp_ether_header *eh;
 
-       if (pkt.input)
+       if (pkt.input) {
                kdp_panic("kdp_send");
+       }
 
-       pkt.off -= (unsigned int)sizeof (struct kdp_udpiphdr);
+       pkt.off -= (unsigned int)sizeof(struct kdp_udpiphdr);
 
 #if DO_ALIGN
        bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
@@ -621,7 +645,7 @@ kdp_send(
        ui->ui_next = ui->ui_prev = 0;
        ui->ui_x1 = 0;
        ui->ui_pr = IPPROTO_UDP;
-       ui->ui_len = htons((u_short)pkt.len + sizeof (struct kdp_udphdr));
+       ui->ui_len = htons((u_short)pkt.len + sizeof(struct kdp_udphdr));
        ui->ui_src = adr.loc.in;
        ui->ui_dst = adr.rmt.in;
        ui->ui_sport = htons(KDP_REMOTE_PORT);
@@ -634,10 +658,10 @@ kdp_send(
 #else
        ip = (struct kdp_ip *)&pkt.data[pkt.off];
 #endif
-       ip->ip_len = htons(sizeof (struct kdp_udpiphdr) + pkt.len);
+       ip->ip_len = htons(sizeof(struct kdp_udpiphdr) + pkt.len);
        ip->ip_v = IPVERSION;
        ip->ip_id = htons(ip_id++);
-       ip->ip_hl = sizeof (struct kdp_ip) >> 2;
+       ip->ip_hl = sizeof(struct kdp_ip) >> 2;
        ip->ip_ttl = udp_ttl;
        ip->ip_sum = 0;
        ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
@@ -645,16 +669,16 @@ kdp_send(
        bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
 #endif
 
-       pkt.len += (unsigned int)sizeof (struct kdp_udpiphdr);
+       pkt.len += (unsigned int)sizeof(struct kdp_udpiphdr);
 
-       pkt.off -= (unsigned int)sizeof (struct kdp_ether_header);
+       pkt.off -= (unsigned int)sizeof(struct kdp_ether_header);
 
        eh = (struct kdp_ether_header *)&pkt.data[pkt.off];
        enaddr_copy(&adr.loc.ea, eh->ether_shost);
        enaddr_copy(&adr.rmt.ea, eh->ether_dhost);
        eh->ether_type = htons(ETHERTYPE_IP);
 
-       pkt.len += (unsigned int)sizeof (struct kdp_ether_header);
+       pkt.len += (unsigned int)sizeof(struct kdp_ether_header);
        kdp_send_data(&pkt.data[pkt.off], pkt.len);
 }
 
@@ -664,58 +688,62 @@ debugger_if_necessary(void)
 {
        if ((current_debugger == KDP_CUR_DB) && halt_in_debugger) {
                kdp_call();
-               halt_in_debugger=0;
+               halt_in_debugger = 0;
        }
 }
 
 
 /* We don't interpret this pointer, we just give it to the bsd stack
-   so it can decide when to set the MAC and IP info. We'll
-   early initialize the MAC/IP info if we can so that we can use
-   KDP early in boot. These values may subsequently get over-written
-   when the interface gets initialized for real.
-*/
*  so it can decide when to set the MAC and IP info. We'll
*  early initialize the MAC/IP info if we can so that we can use
*  KDP early in boot. These values may subsequently get over-written
*  when the interface gets initialized for real.
+ */
 void
 kdp_set_interface(void *ifp, const struct kdp_ether_addr *macaddr)
 {
        char kdpstr[80];
-        struct kdp_in_addr addr = { 0 };
-        unsigned int len;
-        
+       struct kdp_in_addr addr = { 0 };
+       unsigned int len;
+
        kdp_current_ifp = ifp;
 
-        if (PE_parse_boot_argn("kdp_ip_addr", kdpstr, sizeof(kdpstr))) {
-            /* look for a static ip address */
-            if (inet_aton(kdpstr, &addr) == FALSE)
-                goto done;
+       if (PE_parse_boot_argn("kdp_ip_addr", kdpstr, sizeof(kdpstr))) {
+               /* look for a static ip address */
+               if (inet_aton(kdpstr, &addr) == FALSE) {
+                       goto done;
+               }
 
-            goto config_network;
-        }
+               goto config_network;
+       }
 
-        /* use saved ip address */
-        save_ip_in_nvram = TRUE;
+       /* use saved ip address */
+       save_ip_in_nvram = TRUE;
 
-        len = sizeof(kdpstr);
-        if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr, &len) == FALSE)
-            goto done;
+       len = sizeof(kdpstr);
+       if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr, &len) == FALSE) {
+               goto done;
+       }
 
-        kdpstr[len < sizeof(kdpstr) ? len : sizeof(kdpstr) - 1] = '\0';
-        if (inet_aton(kdpstr, &addr) == FALSE)
-            goto done;
+       kdpstr[len < sizeof(kdpstr) ? len : sizeof(kdpstr) - 1] = '\0';
+       if (inet_aton(kdpstr, &addr) == FALSE) {
+               goto done;
+       }
 
 config_network:
-        kdp_current_ip_address = addr.s_addr;
-        if (macaddr)
-            kdp_current_mac_address = *macaddr;
-
-        /* we can't drop into the debugger at this point because the
-           link will likely not be up. when getDebuggerLinkStatus() support gets
-           added to the appropriate network drivers, adding the
-           following will enable this capability:
-           debugger_if_necessary();
-        */
+       kdp_current_ip_address = addr.s_addr;
+       if (macaddr) {
+               kdp_current_mac_address = *macaddr;
+       }
+
+       /* we can't drop into the debugger at this point because the
+        *  link will likely not be up. when getDebuggerLinkStatus() support gets
+        *  added to the appropriate network drivers, adding the
+        *  following will enable this capability:
+        *  debugger_if_necessary();
+        */
 done:
-        return;
+       return;
 }
 
 void *
@@ -724,18 +752,19 @@ kdp_get_interface(void)
        return kdp_current_ifp;
 }
 
-void 
+void
 kdp_set_ip_and_mac_addresses(
-       struct kdp_in_addr              *ipaddr, 
-       struct kdp_ether_addr   *macaddr)
+       struct kdp_in_addr              *ipaddr,
+       struct kdp_ether_addr   *macaddr)
 {
        static uint64_t last_time    = (uint64_t) -1;
        static uint64_t throttle_val = 0;
        uint64_t cur_time;
        char addr[16];
 
-       if (kdp_current_ip_address == ipaddr->s_addr)
+       if (kdp_current_ip_address == ipaddr->s_addr) {
                goto done;
+       }
 
        /* don't replace if serial debugging is configured */
        if (!KDP_SERIAL_ENABLED() ||
@@ -744,21 +773,24 @@ kdp_set_ip_and_mac_addresses(
                kdp_current_ip_address  = ipaddr->s_addr;
        }
 
-       if (save_ip_in_nvram == FALSE)
+       if (save_ip_in_nvram == FALSE) {
                goto done;
+       }
 
-       if (inet_ntoa_r(*ipaddr, addr, sizeof(addr)) == NULL)
+       if (inet_ntoa_r(*ipaddr, addr, sizeof(addr)) == NULL) {
                goto done;
+       }
 
        /* throttle writes if needed */
-       if (!throttle_val)
+       if (!throttle_val) {
                nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE, &throttle_val);
+       }
 
        cur_time = mach_absolute_time();
        if (last_time == (uint64_t) -1 ||
            ((cur_time - last_time) > throttle_val)) {
                PEWriteNVRAMProperty("_kdp_ipstr", addr,
-                               (const unsigned int) strlen(addr));
+                   (const unsigned int) strlen(addr));
        }
        last_time = cur_time;
 
@@ -771,15 +803,15 @@ kdp_set_gateway_mac(void *gatewaymac)
 {
        router_mac = *(struct kdp_ether_addr *)gatewaymac;
        flag_router_mac_initialized = TRUE;
-} 
+}
 
-struct kdp_ether_addr 
+struct kdp_ether_addr
 kdp_get_mac_addr(void)
 {
        return kdp_current_mac_address;
 }
 
-unsigned int 
+unsigned int
 kdp_get_ip_address(void)
 {
        return (unsigned int)kdp_current_ip_address;
@@ -794,13 +826,13 @@ kdp_disable_arp(void)
 static void
 kdp_arp_dispatch(void)
 {
-       struct kdp_ether_arp    aligned_ea, *ea = &aligned_ea;
-       unsigned                arp_header_offset;
+       struct kdp_ether_arp    aligned_ea, *ea = &aligned_ea;
+       unsigned                arp_header_offset;
 
        arp_header_offset = (unsigned)sizeof(struct kdp_ether_header) + pkt.off;
        memcpy((void *)ea, (void *)&pkt.data[arp_header_offset], sizeof(*ea));
 
-       switch(ntohs(ea->arp_op)) {
+       switch (ntohs(ea->arp_op)) {
        case ARPOP_REQUEST:
                kdp_arp_reply(ea);
                break;
@@ -816,12 +848,14 @@ static void
 kdp_process_arp_reply(struct kdp_ether_arp *ea)
 {
        /* Are we interested in ARP replies? */
-       if (flag_arp_resolved == TRUE)
+       if (flag_arp_resolved == TRUE) {
                return;
+       }
 
        /* Did we receive a reply from the right source? */
-       if (((struct kdp_in_addr *)(ea->arp_spa))->s_addr != target_ip)
-         return;
+       if (((struct kdp_in_addr *)(ea->arp_spa))->s_addr != target_ip) {
+               return;
+       }
 
        flag_arp_resolved = TRUE;
        current_resolved_MAC = *(struct kdp_ether_addr *) (ea->arp_sha);
@@ -833,35 +867,37 @@ kdp_process_arp_reply(struct kdp_ether_arp *ea)
  * is set.
  */
 
-static void 
+static void
 kdp_arp_reply(struct kdp_ether_arp *ea)
 {
-       struct kdp_ether_header *eh;
+       struct kdp_ether_header *eh;
 
-       struct kdp_in_addr              isaddr, itaddr, myaddr;
-       struct kdp_ether_addr   my_enaddr;
+       struct kdp_in_addr              isaddr, itaddr, myaddr;
+       struct kdp_ether_addr   my_enaddr;
 
        eh = (struct kdp_ether_header *)&pkt.data[pkt.off];
        pkt.off += (unsigned int)sizeof(struct kdp_ether_header);
 
-       if(ntohs(ea->arp_op) != ARPOP_REQUEST)
-         return;
+       if (ntohs(ea->arp_op) != ARPOP_REQUEST) {
+               return;
+       }
 
        myaddr.s_addr = kdp_get_ip_address();
        my_enaddr = kdp_get_mac_addr();
 
        if ((ntohl(myaddr.s_addr) == 0) ||
            ((my_enaddr.ether_addr_octet[0] & 0xff) == 0
-                && (my_enaddr.ether_addr_octet[1] & 0xff) == 0
-                && (my_enaddr.ether_addr_octet[2] & 0xff) == 0
-                && (my_enaddr.ether_addr_octet[3] & 0xff) == 0
-                && (my_enaddr.ether_addr_octet[4] & 0xff) == 0
-                && (my_enaddr.ether_addr_octet[5] & 0xff) == 0
-                ))
+           && (my_enaddr.ether_addr_octet[1] & 0xff) == 0
+           && (my_enaddr.ether_addr_octet[2] & 0xff) == 0
+           && (my_enaddr.ether_addr_octet[3] & 0xff) == 0
+           && (my_enaddr.ether_addr_octet[4] & 0xff) == 0
+           && (my_enaddr.ether_addr_octet[5] & 0xff) == 0
+           )) {
                return;
+       }
 
-       (void)memcpy((void *)&isaddr, (void *)ea->arp_spa, sizeof (isaddr));
-       (void)memcpy((void *)&itaddr, (void *)ea->arp_tpa, sizeof (itaddr));
+       (void)memcpy((void *)&isaddr, (void *)ea->arp_spa, sizeof(isaddr));
+       (void)memcpy((void *)&itaddr, (void *)ea->arp_tpa, sizeof(itaddr));
 
        if (itaddr.s_addr == myaddr.s_addr) {
                (void)memcpy((void *)ea->arp_tha, (void *)ea->arp_sha, sizeof(ea->arp_sha));
@@ -871,12 +907,12 @@ kdp_arp_reply(struct kdp_ether_arp *ea)
                (void)memcpy((void *)ea->arp_spa, (void *) &itaddr, sizeof(ea->arp_spa));
 
                ea->arp_op = htons(ARPOP_REPLY);
-               ea->arp_pro = htons(ETHERTYPE_IP); 
+               ea->arp_pro = htons(ETHERTYPE_IP);
                (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
                (void)memcpy(eh->ether_shost, &my_enaddr, sizeof(eh->ether_shost));
                eh->ether_type = htons(ETHERTYPE_ARP);
                (void)memcpy(&pkt.data[pkt.off], ea, sizeof(*ea));
-               pkt.off -= (unsigned int)sizeof (struct kdp_ether_header);
+               pkt.off -= (unsigned int)sizeof(struct kdp_ether_header);
                /* pkt.len is still the length we want, ether_header+ether_arp */
                kdp_send_data(&pkt.data[pkt.off], pkt.len);
        }
@@ -885,16 +921,17 @@ kdp_arp_reply(struct kdp_ether_arp *ea)
 static void
 kdp_poll(void)
 {
-       struct kdp_ether_header *eh = NULL;
-       struct kdp_udpiphdr     aligned_ui, *ui = &aligned_ui;
-       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
-       static int              msg_printed;
+       struct kdp_ether_header *eh = NULL;
+       struct kdp_udpiphdr     aligned_ui, *ui = &aligned_ui;
+       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
+       static int              msg_printed;
 
-       if (pkt.input)
+       if (pkt.input) {
                kdp_panic("kdp_poll");
+       }
+
        if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
-               ifmsg_printed == 0) {
+               if (msg_printed == 0) {
                        msg_printed = 1;
                        printf("kdp_poll: no debugger device\n");
                }
@@ -902,14 +939,15 @@ kdp_poll(void)
        }
 
        pkt.off = pkt.len = 0;
-       kdp_receive_data(pkt.data, &pkt.len, 3/* ms */);
+       kdp_receive_data(pkt.data, &pkt.len, 3 /* ms */);
 
-       if (pkt.len == 0)
+       if (pkt.len == 0) {
                return;
+       }
 
        if (pkt.len >= sizeof(struct kdp_ether_header)) {
-               eh = (struct kdp_ether_header *)&pkt.data[pkt.off];  
-       
+               eh = (struct kdp_ether_header *)&pkt.data[pkt.off];
+
                if (kdp_flag & KDP_ARP) {
                        if (ntohs(eh->ether_type) == ETHERTYPE_ARP) {
                                kdp_arp_dispatch();
@@ -918,10 +956,11 @@ kdp_poll(void)
                }
        }
 
-       if (pkt.len < (sizeof (struct kdp_ether_header) + sizeof (struct kdp_udpiphdr)))
+       if (pkt.len < (sizeof(struct kdp_ether_header) + sizeof(struct kdp_udpiphdr))) {
                return;
+       }
 
-       pkt.off += (unsigned int)sizeof (struct kdp_ether_header);
+       pkt.off += (unsigned int)sizeof(struct kdp_ether_header);
        if (ntohs(eh->ether_type) != ETHERTYPE_IP) {
                return;
        }
@@ -934,34 +973,33 @@ kdp_poll(void)
        ip = (struct kdp_ip *)&pkt.data[pkt.off];
 #endif
 
-       pkt.off += (unsigned int)sizeof (struct kdp_udpiphdr);
+       pkt.off += (unsigned int)sizeof(struct kdp_udpiphdr);
        if (ui->ui_pr != IPPROTO_UDP) {
                return;
        }
-       if (ip->ip_hl > (sizeof (struct kdp_ip) >> 2)) {
+
+       if (ip->ip_hl > (sizeof(struct kdp_ip) >> 2)) {
                return;
        }
 
        if (ntohs(ui->ui_dport) != KDP_REMOTE_PORT) {
-               if (panicd_port == (ntohs(ui->ui_dport)) && 
+               if (panicd_port == (ntohs(ui->ui_dport)) &&
                    flag_panic_dump_in_progress) {
                        last_panic_port = ui->ui_sport;
-               }
-               else
+               } else {
                        return;
+               }
        }
-       /* If we receive a kernel debugging packet whilst a 
-        * core dump is in progress, abort the transfer and 
-        * enter the debugger if not told otherwise. 
+       /* If we receive a kernel debugging packet whilst a
+        * core dump is in progress, abort the transfer and
+        * enter the debugger if not told otherwise.
         */
-       else
-               if (flag_panic_dump_in_progress) {
-                       if (!flag_dont_abort_panic_dump) {
-                               abort_panic_transfer();
-                       }
-                       return;
+       else if (flag_panic_dump_in_progress) {
+               if (!flag_dont_abort_panic_dump) {
+                       abort_panic_transfer();
                }
+               return;
+       }
 
        if (!kdp.is_conn && !flag_panic_dump_in_progress) {
                enaddr_copy(eh->ether_dhost, &adr.loc.ea);
@@ -974,7 +1012,7 @@ kdp_poll(void)
        /*
         * Calculate kdp packet length.
         */
-       pkt.len = ntohs((u_short)ui->ui_ulen) - (unsigned int)sizeof (struct kdp_udphdr);
+       pkt.len = ntohs((u_short)ui->ui_ulen) - (unsigned int)sizeof(struct kdp_udphdr);
        pkt.input = TRUE;
 }
 
@@ -986,10 +1024,10 @@ kdp_poll(void)
 static void
 transmit_ARP_request(uint32_t ip_addr)
 {
-       struct kdp_ether_header *eh = (struct kdp_ether_header *) &pkt.data[0];
-       struct kdp_ether_arp    *ea = (struct kdp_ether_arp *) &pkt.data[sizeof(struct kdp_ether_header)];
+       struct kdp_ether_header *eh = (struct kdp_ether_header *) &pkt.data[0];
+       struct kdp_ether_arp    *ea = (struct kdp_ether_arp *) &pkt.data[sizeof(struct kdp_ether_header)];
 
-       KDP_DEBUG("Transmitting ARP request\n");
+       KDP_DEBUG("Transmitting ARP request\n");
        /* Populate the ether_header */
        eh->ether_type = htons(ETHERTYPE_ARP);
        enaddr_copy(&kdp_current_mac_address, eh->ether_shost);
@@ -1048,7 +1086,7 @@ TRANSMIT_RETRY:
                *resolved_MAC = current_resolved_MAC;
                return TRUE;
        }
-       
+
        if (!flag_panic_dump_in_progress || pkt.input) { /* we received a debugging packet, bail*/
                printf("Received a debugger packet,transferring control to debugger\n");
                /* Indicate that we should wait in the debugger when we return */
@@ -1066,17 +1104,18 @@ TRANSMIT_RETRY:
 
 static void
 kdp_handler(
-       void    *saved_state
-)
+       void    *saved_state
+       )
 {
-       unsigned short          reply_port;
-       kdp_hdr_t               aligned_hdr, *hdr = &aligned_hdr;
+       unsigned short          reply_port;
+       kdp_hdr_t               aligned_hdr, *hdr = &aligned_hdr;
 
        kdp.saved_state = saved_state;  // see comment in kdp_raise_exception
 
        do {
-               while (!pkt.input)
+               while (!pkt.input) {
                        kdp_poll();
+               }
 
 #if DO_ALIGN
                bcopy((char *)&pkt.data[pkt.off], (char *)hdr, sizeof(*hdr));
@@ -1089,24 +1128,25 @@ kdp_handler(
                        goto again;
                }
 
-               if (hdr->request == KDP_REATTACH)
+               if (hdr->request == KDP_REATTACH) {
                        exception_seq = hdr->seq;
+               }
 
                // check for retransmitted request
                if (hdr->seq == (exception_seq - 1)) {
                        /* retransmit last reply */
                        kdp_send_data(&saved_reply.data[saved_reply.off],
-                                       saved_reply.len);
+                           saved_reply.len);
                        goto again;
                } else if ((hdr->seq != exception_seq) &&
-                               (hdr->request != KDP_CONNECT)) {
+                   (hdr->request != KDP_CONNECT)) {
                        printf("kdp: bad sequence %d (want %d)\n",
-                                       hdr->seq, exception_seq);
+                           hdr->seq, exception_seq);
                        goto again;
                }
 
                /* This is a manual side-channel to the main KDP protocol.
-                * A client like GDB/kgmacros can manually construct 
+                * A client like GDB/kgmacros can manually construct
                 * a request, set the input flag, issue a dummy KDP request,
                 * and then manually collect the result
                 */
@@ -1117,24 +1157,24 @@ kdp_handler(
                                /* process */
                                int packet_length = manual_pkt.len;
                                kdp_packet((unsigned char *)&manual_pkt.data,
-                                               &packet_length,
-                                               &manual_port_unused);
+                                   &packet_length,
+                                   &manual_port_unused);
                                manual_pkt.len = packet_length;
                        }
                        manual_pkt.input = 0;
                }
 
                if (kdp_packet((unsigned char*)&pkt.data[pkt.off],
-                                       (int *)&pkt.len,
-                                       (unsigned short *)&reply_port)) {
+                   (int *)&pkt.len,
+                   (unsigned short *)&reply_port)) {
                        boolean_t sideband = FALSE;
 
                        /* if it's an already connected error message,
-                        * send a sideband reply for that. for successful connects,
-                        * make sure the sequence number is correct. */
+                       * send a sideband reply for that. for successful connects,
+                       * make sure the sequence number is correct. */
                        if (hdr->request == KDP_CONNECT) {
                                kdp_connect_reply_t *rp =
-                                       (kdp_connect_reply_t *) &pkt.data[pkt.off];
+                                   (kdp_connect_reply_t *) &pkt.data[pkt.off];
                                kdp_error_t err = rp->error;
 
                                if (err == KDPERR_NO_ERROR) {
@@ -1155,9 +1195,9 @@ again:
 static void
 kdp_connection_wait(void)
 {
-       unsigned short          reply_port;
-       struct kdp_ether_addr   kdp_mac_addr = kdp_get_mac_addr();
-       unsigned int            ip_addr = ntohl(kdp_get_ip_address());
+       unsigned short          reply_port;
+       struct kdp_ether_addr   kdp_mac_addr = kdp_get_mac_addr();
+       unsigned int            ip_addr = ntohl(kdp_get_ip_address());
 
        /*
         * Do both a printf() and a kprintf() of the MAC and IP so that
@@ -1170,34 +1210,34 @@ kdp_connection_wait(void)
                kprintf("Using serial KDP.\n");
        } else {
                printf("ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
-                       kdp_mac_addr.ether_addr_octet[0] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[1] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[2] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[3] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[4] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[5] & 0xff);
+                   kdp_mac_addr.ether_addr_octet[0] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[1] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[2] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[3] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[4] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[5] & 0xff);
 
                kprintf("ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
-                       kdp_mac_addr.ether_addr_octet[0] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[1] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[2] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[3] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[4] & 0xff,
-                       kdp_mac_addr.ether_addr_octet[5] & 0xff);
+                   kdp_mac_addr.ether_addr_octet[0] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[1] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[2] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[3] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[4] & 0xff,
+                   kdp_mac_addr.ether_addr_octet[5] & 0xff);
 
                printf("ip address: %d.%d.%d.%d\n",
-                       (ip_addr & 0xff000000) >> 24,
-                       (ip_addr & 0xff0000) >> 16,
-                       (ip_addr & 0xff00) >> 8,
-                       (ip_addr & 0xff));
+                   (ip_addr & 0xff000000) >> 24,
+                   (ip_addr & 0xff0000) >> 16,
+                   (ip_addr & 0xff00) >> 8,
+                   (ip_addr & 0xff));
 
                kprintf("ip address: %d.%d.%d.%d\n",
-                       (ip_addr & 0xff000000) >> 24,
-                       (ip_addr & 0xff0000) >> 16,
-                       (ip_addr & 0xff00) >> 8,
-                       (ip_addr & 0xff));
+                   (ip_addr & 0xff000000) >> 24,
+                   (ip_addr & 0xff0000) >> 16,
+                   (ip_addr & 0xff00) >> 8,
+                   (ip_addr & 0xff));
        }
-            
+
        printf("\nWaiting for remote debugger connection.\n");
        kprintf("\nWaiting for remote debugger connection.\n");
 
@@ -1206,7 +1246,7 @@ kdp_connection_wait(void)
 #endif
 
        if (reattach_wait == 0) {
-               if((kdp_flag & KDP_GETC_ENA) && (0 != kdp_getc())) {
+               if ((kdp_flag & KDP_GETC_ENA) && (0 != kdp_getc())) {
                        printf("Options.....    Type\n");
                        printf("------------    ----\n");
                        printf("continue....    'c'\n");
@@ -1215,15 +1255,15 @@ kdp_connection_wait(void)
        } else {
                reattach_wait = 0;
        }
-    
+
        exception_seq = 0;
 
        do {
                kdp_hdr_t aligned_hdr, *hdr = &aligned_hdr;
-       
+
                while (!pkt.input) {
                        if (kdp_flag & KDP_GETC_ENA) {
-                               switch(kdp_getc()) {
+                               switch (kdp_getc()) {
                                case 'c':
                                        printf("Continuing...\n");
                                        return;
@@ -1233,7 +1273,7 @@ kdp_connection_wait(void)
                                        break;
                                default:
                                        break;
-                               }
+                               }
                        }
                        kdp_poll();
                }
@@ -1248,46 +1288,48 @@ kdp_connection_wait(void)
                        /* should not return! */
                }
                if (((hdr->request == KDP_CONNECT) || (hdr->request == KDP_REATTACH)) &&
-                       !hdr->is_reply && (hdr->seq == exception_seq)) {
-                   if (kdp_packet((unsigned char *)&pkt.data[pkt.off],
-                               (int *)&pkt.len,
-                               (unsigned short *)&reply_port))
-                           kdp_reply(reply_port, FALSE);
-                   if (hdr->request == KDP_REATTACH) {
+                   !hdr->is_reply && (hdr->seq == exception_seq)) {
+                       if (kdp_packet((unsigned char *)&pkt.data[pkt.off],
+                           (int *)&pkt.len,
+                           (unsigned short *)&reply_port)) {
+                               kdp_reply(reply_port, FALSE);
+                       }
+                       if (hdr->request == KDP_REATTACH) {
                                reattach_wait = 0;
-                               hdr->request=KDP_DISCONNECT;
+                               hdr->request = KDP_DISCONNECT;
                                exception_seq = 0;
                        }
                }
 
                pkt.input = FALSE;
        } while (!kdp.is_conn);
-    
-       if (current_debugger == KDP_CUR_DB)
-               active_debugger=1;
+
+       if (current_debugger == KDP_CUR_DB) {
+               active_debugger = 1;
+       }
        printf("Connected to remote debugger.\n");
        kprintf("Connected to remote debugger.\n");
 }
 
 static void
 kdp_send_exception(
-       unsigned int            exception,
-       unsigned int            code,
-       unsigned int            subcode
-)
+       unsigned int            exception,
+       unsigned int            code,
+       unsigned int            subcode
+       )
 {
-       unsigned short          remote_port;
-       unsigned int            timeout_count = 100;
-       unsigned int            poll_timeout;
+       unsigned short          remote_port;
+       unsigned int            timeout_count = 100;
+       unsigned int            poll_timeout;
 
        do {
-               pkt.off = sizeof (struct kdp_ether_header) + sizeof (struct kdp_udpiphdr);
+               pkt.off = sizeof(struct kdp_ether_header) + sizeof(struct kdp_udpiphdr);
                kdp_exception((unsigned char *)&pkt.data[pkt.off],
-                               (int *)&pkt.len,
-                               (unsigned short *)&remote_port,
-                               (unsigned int)exception,
-                               (unsigned int)code,
-                               (unsigned int)subcode);
+                   (int *)&pkt.len,
+                   (unsigned short *)&remote_port,
+                   (unsigned int)exception,
+                   (unsigned int)code,
+                   (unsigned int)subcode);
 
                kdp_send(remote_port);
 
@@ -1305,31 +1347,33 @@ kdp_send_exception(
 
                pkt.input = FALSE;
 
-               if (kdp.exception_ack_needed)
+               if (kdp.exception_ack_needed) {
                        kdp_us_spin(250000);
-
+               }
        } while (kdp.exception_ack_needed && timeout_count--);
 
        if (kdp.exception_ack_needed) {
                // give up & disconnect
                printf("kdp: exception ack timeout\n");
-               if (current_debugger == KDP_CUR_DB)
-                       active_debugger=0;
+               if (current_debugger == KDP_CUR_DB) {
+                       active_debugger = 0;
+               }
                kdp_reset();
        }
 }
 
-static void 
+static void
 kdp_debugger_loop(
-       unsigned int            exception,
-       unsigned int            code,
-       unsigned int            subcode,
-       void                    *saved_state)
+       unsigned int            exception,
+       unsigned int            code,
+       unsigned int            subcode,
+       void                    *saved_state)
 {
-       int                     index;
+       int                     index;
 
-       if (saved_state == 0) 
+       if (saved_state == 0) {
                printf("kdp_raise_exception with NULL state\n");
+       }
 
        index = exception;
        if (exception != EXC_BREAKPOINT) {
@@ -1337,8 +1381,8 @@ kdp_debugger_loop(
                        index = 0;
                }
                printf("%s exception (%x,%x,%x)\n",
-                               exception_message[index],
-                               exception, code, subcode);
+                   exception_message[index],
+                   exception, code, subcode);
        }
 
        kdp_sync_cache();
@@ -1350,29 +1394,32 @@ kdp_debugger_loop(
        kdp.kdp_cpu = cpu_number();
        kdp.kdp_thread = current_thread();
 
-       if (kdp_en_setmode)
+       if (kdp_en_setmode) {
                (*kdp_en_setmode)(TRUE); /* enabling link mode */
-
-       if (pkt.input)
+       }
+       if (pkt.input) {
                kdp_panic("kdp_raise_exception");
+       }
 
        if (((kdp_flag & KDP_PANIC_DUMP_ENABLED)
-            || (kdp_flag & PANIC_LOG_DUMP))
+           || (kdp_flag & PANIC_LOG_DUMP))
            && panic_active()) {
                kdp_panic_dump();
-               if (kdp_flag & REBOOT_POST_CORE && dumped_kernel_core())
+               if (kdp_flag & REBOOT_POST_CORE && dumped_kernel_core()) {
                        kdp_machine_reboot();
+               }
        } else {
                if ((kdp_flag & PANIC_CORE_ON_NMI) && !panic_active()
-                       && !kdp.is_conn) {
-
+                   && !kdp.is_conn) {
                        disableConsoleOutput = FALSE;
                        kdp_panic_dump();
-                       if (kdp_flag & REBOOT_POST_CORE && dumped_kernel_core())
+                       if (kdp_flag & REBOOT_POST_CORE && dumped_kernel_core()) {
                                kdp_machine_reboot();
+                       }
 
-                       if (!(kdp_flag & DBG_POST_CORE))
+                       if (!(kdp_flag & DBG_POST_CORE)) {
                                goto exit_debugger_loop;
+                       }
                }
        }
 
@@ -1389,10 +1436,9 @@ again:
        }
 
        if (kdp.is_conn) {
-               kdp.is_halted = TRUE;           /* XXX */
+               kdp.is_halted = TRUE;           /* XXX */
                kdp_handler(saved_state);
-               if (!kdp.is_conn)
-               {
+               if (!kdp.is_conn) {
                        kdp_remove_all_breakpoints();
                        printf("Remote debugger disconnected.\n");
                }
@@ -1405,8 +1451,9 @@ again:
        if (1 == kdp_trigger_core_dump) {
                kdp_flag |= KDP_PANIC_DUMP_ENABLED;
                kdp_panic_dump();
-               if (kdp_flag & REBOOT_POST_CORE && dumped_kernel_core())
+               if (kdp_flag & REBOOT_POST_CORE && dumped_kernel_core()) {
                        kdp_machine_reboot();
+               }
                kdp_trigger_core_dump = 0;
        }
 
@@ -1429,12 +1476,14 @@ again:
 
        kdp_sync_cache();
 
-       if (reattach_wait == 1)
+       if (reattach_wait == 1) {
                goto again;
+       }
 
 exit_debugger_loop:
-       if (kdp_en_setmode)
+       if (kdp_en_setmode) {
                (*kdp_en_setmode)(FALSE); /* link cleanup */
+       }
 }
 
 void
@@ -1449,21 +1498,21 @@ kdp_reset(void)
 }
 
 struct corehdr *
-create_panic_header(unsigned int request, const char *corename, 
-                       unsigned length, unsigned int block)
+create_panic_header(unsigned int request, const char *corename,
+    unsigned length, unsigned int block)
 {
-       struct kdp_udpiphdr     aligned_ui, *ui = &aligned_ui;
-       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
-       struct kdp_ether_header *eh;
-       struct corehdr          *coreh;
-       const char              *mode = "octet";
-       char                    modelen  = strlen(mode) + 1;
+       struct kdp_udpiphdr     aligned_ui, *ui = &aligned_ui;
+       struct kdp_ip           aligned_ip, *ip = &aligned_ip;
+       struct kdp_ether_header *eh;
+       struct corehdr          *coreh;
+       const char              *mode = "octet";
+       char                    modelen  = strlen(mode) + 1;
 
-       size_t                  fmask_size = sizeof(KDP_FEATURE_MASK_STRING) + sizeof(kdp_crashdump_feature_mask);
+       size_t                  fmask_size = sizeof(KDP_FEATURE_MASK_STRING) + sizeof(kdp_crashdump_feature_mask);
 
-       pkt.off = sizeof (struct kdp_ether_header);
-       pkt.len = (unsigned int)(length + ((request == KDP_WRQ) ? modelen + fmask_size : 0) + 
-       (corename ? (strlen(corename) + 1 ): 0) + sizeof(struct corehdr));
+       pkt.off = sizeof(struct kdp_ether_header);
+       pkt.len = (unsigned int)(length + ((request == KDP_WRQ) ? modelen + fmask_size : 0) +
+           (corename ? (strlen(corename) + 1): 0) + sizeof(struct corehdr));
 
 #if DO_ALIGN
        bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
@@ -1473,7 +1522,7 @@ create_panic_header(unsigned int request, const char *corename,
        ui->ui_next = ui->ui_prev = 0;
        ui->ui_x1 = 0;
        ui->ui_pr = IPPROTO_UDP;
-       ui->ui_len = htons((u_short)pkt.len + sizeof (struct kdp_udphdr));
+       ui->ui_len = htons((u_short)pkt.len + sizeof(struct kdp_udphdr));
        ui->ui_src.s_addr = (uint32_t)kdp_current_ip_address;
        /* Already in network byte order via inet_aton() */
        ui->ui_dst.s_addr = panic_server_ip;
@@ -1487,31 +1536,31 @@ create_panic_header(unsigned int request, const char *corename,
 #else
        ip = (struct kdp_ip *)&pkt.data[pkt.off];
 #endif
-       ip->ip_len = htons(sizeof (struct kdp_udpiphdr) + pkt.len);
+       ip->ip_len = htons(sizeof(struct kdp_udpiphdr) + pkt.len);
        ip->ip_v = IPVERSION;
        ip->ip_id = htons(ip_id++);
-       ip->ip_hl = sizeof (struct kdp_ip) >> 2;
+       ip->ip_hl = sizeof(struct kdp_ip) >> 2;
        ip->ip_ttl = udp_ttl;
        ip->ip_sum = 0;
        ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
 #if DO_ALIGN
        bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
 #endif
-    
-       pkt.len += (unsigned int)sizeof (struct kdp_udpiphdr);
 
-       pkt.off += (unsigned int)sizeof (struct kdp_udpiphdr);
-  
+       pkt.len += (unsigned int)sizeof(struct kdp_udpiphdr);
+
+       pkt.off += (unsigned int)sizeof(struct kdp_udpiphdr);
+
        coreh = (struct corehdr *) &pkt.data[pkt.off];
        coreh->th_opcode = htons((u_short)request);
-  
+
        if (request == KDP_WRQ) {
                char *cp;
 
                cp = coreh->th_u.tu_rpl;
-               cp += strlcpy (cp, corename, KDP_MAXPACKET);
+               cp += strlcpy(cp, corename, KDP_MAXPACKET);
                *cp++ = '\0';
-               cp += strlcpy (cp, mode, KDP_MAXPACKET - strlen(corename));
+               cp += strlcpy(cp, mode, KDP_MAXPACKET - strlen(corename));
                *cp++ = '\0';
                cp += strlcpy(cp, KDP_FEATURE_MASK_STRING, sizeof(KDP_FEATURE_MASK_STRING));
                *cp++ = '\0'; /* Redundant */
@@ -1524,15 +1573,15 @@ create_panic_header(unsigned int request, const char *corename,
                coreh->th_block = htonl((unsigned int) block);
        }
 
-       pkt.off -= (unsigned int)sizeof (struct kdp_udpiphdr);
-       pkt.off -= (unsigned int)sizeof (struct kdp_ether_header);
+       pkt.off -= (unsigned int)sizeof(struct kdp_udpiphdr);
+       pkt.off -= (unsigned int)sizeof(struct kdp_ether_header);
 
        eh = (struct kdp_ether_header *)&pkt.data[pkt.off];
        enaddr_copy(&kdp_current_mac_address, eh->ether_shost);
        enaddr_copy(&destination_mac, eh->ether_dhost);
        eh->ether_type = htons(ETHERTYPE_IP);
-    
-       pkt.len += (unsigned int)sizeof (struct kdp_ether_header);
+
+       pkt.len += (unsigned int)sizeof(struct kdp_ether_header);
        return coreh;
 }
 
@@ -1542,18 +1591,18 @@ kdp_send_crashdump_seek(char *corename, uint64_t seek_off)
        int panic_error;
 
        if (kdp_feature_large_crashdumps) {
-               panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename, 
-                                                    sizeof(seek_off),
-                                                    &seek_off);
+               panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename,
+                   sizeof(seek_off),
+                   &seek_off);
        } else {
                uint32_t off = (uint32_t) seek_off;
-               panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename, 
-                                                    sizeof(off), &off);
+               panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename,
+                   sizeof(off), &off);
        }
 
        if (panic_error < 0) {
-               printf ("kdp_send_crashdump_pkt failed with error %d\n",
-                       panic_error);
+               printf("kdp_send_crashdump_pkt failed with error %d\n",
+                   panic_error);
                return panic_error;
        }
 
@@ -1562,7 +1611,7 @@ kdp_send_crashdump_seek(char *corename, uint64_t seek_off)
 
 int
 kdp_send_crashdump_data(unsigned int request, char *corename,
-                           uint64_t length, void * txstart)
+    uint64_t length, void * txstart)
 {
        int panic_error = 0;
 
@@ -1570,12 +1619,14 @@ kdp_send_crashdump_data(unsigned int request, char *corename,
                uint64_t chunk = MIN(kdp_crashdump_pkt_size, length);
 
                panic_error = kdp_send_crashdump_pkt(request, corename, chunk,
-                                                       txstart);
+                   txstart);
                if (panic_error < 0) {
-                       printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
+                       printf("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
                        return panic_error;
                }
-               if (!txstart) break;
+               if (!txstart) {
+                       break;
+               }
                txstart = (void *)(((uintptr_t) txstart) + chunk);
                length  -= chunk;
        }
@@ -1585,8 +1636,8 @@ kdp_send_crashdump_data(unsigned int request, char *corename,
 uint32_t kdp_crashdump_short_pkt;
 
 int
-kdp_send_crashdump_pkt(unsigned int request, char *corename, 
-                       uint64_t length, void *panic_data)
+kdp_send_crashdump_pkt(unsigned int request, char *corename,
+    uint64_t length, void *panic_data)
 {
        int poll_count;
        struct corehdr *th = NULL;
@@ -1600,25 +1651,27 @@ kdp_send_crashdump_pkt(unsigned int request, char *corename,
        tretries = rretries = 0;
        poll_count = KDP_CRASHDUMP_POLL_COUNT;
        pkt.off = pkt.len = 0;
-       if (request == KDP_WRQ) /* longer timeout for initial request */
+       if (request == KDP_WRQ) /* longer timeout for initial request */
                poll_count += 1000;
+       }
 
 TRANSMIT_RETRY:
        tretries++;
 
-       if (tretries >=15) {
+       if (tretries >= 15) {
 /* The crashdump server is unreachable for some reason. This could be a network
  * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
  * which is a long standing problem with the IOKit polled mode network driver
  * shim which can prevent transmits/receives completely.
  */
-               printf ("Cannot contact panic server, timing out.\n");
-               return (-3);
+               printf("Cannot contact panic server, timing out.\n");
+               return -3;
        }
 
-       if (tretries > 2)
+       if (tretries > 2) {
                printf("TX retry #%d ", tretries );
-  
+       }
+
        th = create_panic_header(request, corename, (unsigned)length, panic_block);
 
        if (request == KDP_DATA) {
@@ -1627,8 +1680,8 @@ TRANSMIT_RETRY:
                 * bits aren't confusing. */
                if (length < kdp_crashdump_pkt_size) {
                        kdp_crashdump_short_pkt++;
-                       memset(th->th_data + length, 'Y', 
-                               kdp_crashdump_pkt_size - (uint32_t) length);
+                       memset(th->th_data + length, 'Y',
+                           kdp_crashdump_pkt_size - (uint32_t) length);
                }
 
                if (!kdp_machine_vm_read((mach_vm_address_t)(uintptr_t)panic_data, (caddr_t) th->th_data, length)) {
@@ -1641,12 +1694,12 @@ TRANSMIT_RETRY:
                                }
                        }
                }
-       }
-       else if (request == KDP_SEEK) {
-               if (kdp_feature_large_crashdumps)
+       } else if (request == KDP_SEEK) {
+               if (kdp_feature_large_crashdumps) {
                        *(uint64_t *) th->th_data = OSSwapHostToBigInt64((*(uint64_t *) panic_data));
-               else
+               } else {
                        *(unsigned int *) th->th_data = htonl(*(unsigned int *) panic_data);
+               }
        }
 
        kdp_send_data(&pkt.data[pkt.off], pkt.len);
@@ -1660,17 +1713,16 @@ RECEIVE_RETRY:
 
        if (pkt.input) {
                pkt.input = FALSE;
-    
+
                th = (struct corehdr *) &pkt.data[pkt.off];
                if (request == KDP_WRQ) {
                        uint16_t opcode64 = ntohs(th->th_opcode);
-                       uint16_t features64 = (opcode64 & 0xFF00)>>8;
+                       uint16_t features64 = (opcode64 & 0xFF00) >> 8;
                        if ((opcode64 & 0xFF) == KDP_ACK) {
                                kdp_feature_large_crashdumps = features64 & KDP_FEATURE_LARGE_CRASHDUMPS;
                                if (features64 & KDP_FEATURE_LARGE_PKT_SIZE) {
                                        kdp_feature_large_pkt_size = 1;
-                               }
-                               else {
+                               } else {
                                        kdp_feature_large_pkt_size = 0;
                                        kdp_crashdump_pkt_size = 512;
                                }
@@ -1686,24 +1738,25 @@ RECEIVE_RETRY:
                                goto TRANSMIT_RETRY;
                        } else if (ntohl(th->th_block) == (panic_block - 1)) {
                                printf("RX retry ");
-                               if (++rretries > 1)
+                               if (++rretries > 1) {
                                        goto TRANSMIT_RETRY;
-                               else
+                               } else {
                                        goto RECEIVE_RETRY;
+                               }
                        }
                }
        } else if (!flag_panic_dump_in_progress) { /* we received a debugging packet, bail*/
-                       printf("Received a debugger packet,transferring control to debugger\n");
-                       /* Configure that if not set ..*/
-                       kdp_flag |= DBG_POST_CORE;
-                       return (-2);
-               } else { /* We timed out */
-                       if (0 == poll_count) {
-                               poll_count = 1000;
-                               kdp_us_spin ((tretries%4) * panic_timeout); /* capped linear backoff */
-                               goto TRANSMIT_RETRY;
-                       }
+               printf("Received a debugger packet,transferring control to debugger\n");
+               /* Configure that if not set ..*/
+               kdp_flag |= DBG_POST_CORE;
+               return -2;
+       } else {         /* We timed out */
+               if (0 == poll_count) {
+                       poll_count = 1000;
+                       kdp_us_spin((tretries % 4) * panic_timeout);        /* capped linear backoff */
+                       goto TRANSMIT_RETRY;
                }
+       }
 
        if (!(++panic_block % SBLOCKSZ)) {
                uint64_t ctime;
@@ -1711,10 +1764,12 @@ RECEIVE_RETRY:
                ctime = mach_absolute_time();
                kdp_superblock_dump_time = ctime - kdp_superblock_dump_start_time;
                kdp_superblock_dump_start_time = ctime;
-               if (kdp_superblock_dump_time > kdp_max_superblock_dump_time)
+               if (kdp_superblock_dump_time > kdp_max_superblock_dump_time) {
                        kdp_max_superblock_dump_time = kdp_superblock_dump_time;
-               if (kdp_superblock_dump_time < kdp_min_superblock_dump_time)
+               }
+               if (kdp_superblock_dump_time < kdp_min_superblock_dump_time) {
                        kdp_min_superblock_dump_time = kdp_superblock_dump_time;
+               }
        }
 
        if (request == KDP_EOF) {
@@ -1726,10 +1781,10 @@ RECEIVE_RETRY:
        return KERN_SUCCESS;
 }
 
-static int 
-isdigit (char c)
+static int
+isdigit(char c)
 {
-       return ((c > 47) && (c < 58));
+       return (c > 47) && (c < 58);
 }
 
 /* Horrid hack to extract xnu version if possible - a much cleaner approach
@@ -1741,7 +1796,7 @@ isdigit (char c)
 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
  * the minor kernel version numbers from the version string.
  */
-static int 
+static int
 kdp_get_xnu_version(char *versionbuf)
 {
        char *versionpos;
@@ -1755,14 +1810,16 @@ kdp_get_xnu_version(char *versionbuf)
                versionpos = strnstr(versionbuf, "xnu-", 115);
                if (versionpos) {
                        strncpy(vstr, versionpos, sizeof(vstr));
-                       vstr[sizeof(vstr)-1] = '\0';
+                       vstr[sizeof(vstr) - 1] = '\0';
                        vptr = vstr + 4; /* Begin after "xnu-" */
-                       while (*vptr && (isdigit(*vptr) || *vptr == '.'))
+                       while (*vptr && (isdigit(*vptr) || *vptr == '.')) {
                                vptr++;
+                       }
                        *vptr = '\0';
                        /* Remove trailing period, if any */
-                       if (*(--vptr) == '.')
+                       if (*(--vptr) == '.') {
                                *vptr = '\0';
+                       }
                        retval = 0;
                }
        }
@@ -1771,9 +1828,9 @@ kdp_get_xnu_version(char *versionbuf)
 }
 
 void
-kdp_set_dump_info(const uint32_t flags, const char *filename, 
-                  const char *destipstr, const char *routeripstr,
-                  const uint32_t port)
+kdp_set_dump_info(const uint32_t flags, const char *filename,
+    const char *destipstr, const char *routeripstr,
+    const uint32_t port)
 {
        uint32_t cmd;
 
@@ -1794,41 +1851,46 @@ kdp_set_dump_info(const uint32_t flags, const char *filename,
                corename_specified = FALSE;
        }
 
-       if (port) 
+       if (port) {
                panicd_port = port;
+       }
 
-        /* on a disconnect, should we stay in KDP or not? */
-        noresume_on_disconnect = (flags & KDP_DUMPINFO_NORESUME) ? 1 : 0;
+       /* on a disconnect, should we stay in KDP or not? */
+       noresume_on_disconnect = (flags & KDP_DUMPINFO_NORESUME) ? 1 : 0;
 
-       if ((flags & KDP_DUMPINFO_DUMP) == 0)
+       if ((flags & KDP_DUMPINFO_DUMP) == 0) {
                return;
+       }
 
        /* the rest of the commands can modify kdp_flags */
        cmd = flags & KDP_DUMPINFO_MASK;
-        if (cmd == KDP_DUMPINFO_DISABLE) {
+       if (cmd == KDP_DUMPINFO_DISABLE) {
                kdp_flag &= ~KDP_PANIC_DUMP_ENABLED;
                panicd_specified       = 0;
                kdp_trigger_core_dump  = 0;
                return;
-        }
+       }
 
        kdp_flag &= ~REBOOT_POST_CORE;
-       if (flags & KDP_DUMPINFO_REBOOT)
-            kdp_flag |= REBOOT_POST_CORE;
+       if (flags & KDP_DUMPINFO_REBOOT) {
+               kdp_flag |= REBOOT_POST_CORE;
+       }
 
        kdp_flag &= ~PANIC_LOG_DUMP;
-       if (cmd == KDP_DUMPINFO_PANICLOG)
-            kdp_flag |= PANIC_LOG_DUMP;
-       
+       if (cmd == KDP_DUMPINFO_PANICLOG) {
+               kdp_flag |= PANIC_LOG_DUMP;
+       }
+
        kdp_flag &= ~SYSTEM_LOG_DUMP;
-       if (cmd == KDP_DUMPINFO_SYSTEMLOG)
-            kdp_flag |= SYSTEM_LOG_DUMP;
+       if (cmd == KDP_DUMPINFO_SYSTEMLOG) {
+               kdp_flag |= SYSTEM_LOG_DUMP;
+       }
 
        /* trigger a dump */
        kdp_flag |= DBG_POST_CORE;
 
-       flag_dont_abort_panic_dump = (flags & KDP_DUMPINFO_NOINTR) ? 
-               TRUE : FALSE;
+       flag_dont_abort_panic_dump = (flags & KDP_DUMPINFO_NOINTR) ?
+           TRUE : FALSE;
 
        reattach_wait          = 1;
        disableConsoleOutput   = 0;
@@ -1838,41 +1900,46 @@ kdp_set_dump_info(const uint32_t flags, const char *filename,
 void
 kdp_get_dump_info(kdp_dumpinfo_reply_t *rp)
 {
-       if (panicd_specified)
+       if (panicd_specified) {
                strlcpy(rp->destip, panicd_ip_str,
                    sizeof(rp->destip));
-       else
+       } else {
                rp->destip[0] = '\0';
+       }
 
-       if (router_specified)
+       if (router_specified) {
                strlcpy(rp->routerip, router_ip_str,
                    sizeof(rp->routerip));
-       else
+       } else {
                rp->routerip[0] = '\0';
+       }
 
-       if (corename_specified)
+       if (corename_specified) {
                strlcpy(rp->name, corename_str,
                    sizeof(rp->name));
-       else
+       } else {
                rp->name[0] = '\0';
+       }
 
        rp->port = panicd_port;
 
        rp->type = 0;
-       if (!panicd_specified)
+       if (!panicd_specified) {
                rp->type |= KDP_DUMPINFO_DISABLE;
-       else if (kdp_flag & PANIC_LOG_DUMP)
+       } else if (kdp_flag & PANIC_LOG_DUMP) {
                rp->type |= KDP_DUMPINFO_PANICLOG;
-       else
+       } else {
                rp->type |= KDP_DUMPINFO_CORE;
+       }
 
-       if (noresume_on_disconnect)
+       if (noresume_on_disconnect) {
                rp->type |= KDP_DUMPINFO_NORESUME;
+       }
 }
 
 
 /* Primary dispatch routine for the system dump */
-void 
+void
 kdp_panic_dump(void)
 {
        char coreprefix[10];
@@ -1880,13 +1947,13 @@ kdp_panic_dump(void)
        int panic_error;
 
        uint64_t        abstime;
-       uint32_t        current_ip = ntohl((uint32_t)kdp_current_ip_address);
+       uint32_t        current_ip = ntohl((uint32_t)kdp_current_ip_address);
 
        if (flag_panic_dump_in_progress) {
                kdb_printf("System dump aborted.\n");
                goto panic_dump_exit;
        }
-               
+
        printf("Entering system dump routine\n");
 
        if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
@@ -1901,35 +1968,38 @@ kdp_panic_dump(void)
 
        flag_panic_dump_in_progress = TRUE;
 
-       if (pkt.input)
+       if (pkt.input) {
                kdp_panic("kdp_panic_dump: unexpected pending input packet");
+       }
 
        kdp_get_xnu_version((char *) &pkt.data[0]);
 
        if (!corename_specified) {
                coresuffix[0] = 0;
                /* Panic log bit takes precedence over core dump bit */
-               if ((debugger_panic_str != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP))
+               if ((debugger_panic_str != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP)) {
                        strlcpy(coreprefix, "paniclog", sizeof(coreprefix));
-               else if (kdp_flag & SYSTEM_LOG_DUMP)
+               } else if (kdp_flag & SYSTEM_LOG_DUMP) {
                        strlcpy(coreprefix, "systemlog", sizeof(coreprefix));
-               else {
+               else {
                        strlcpy(coreprefix, "core", sizeof(coreprefix));
-                       if (!kdp_corezip_disabled) strlcpy(coresuffix, ".gz", sizeof(coresuffix));
+                       if (!kdp_corezip_disabled) {
+                               strlcpy(coresuffix, ".gz", sizeof(coresuffix));
+                       }
                }
 
                abstime = mach_absolute_time();
                pkt.data[20] = '\0';
-               snprintf (corename_str,
-                               sizeof(corename_str),
-                               "%s-%s-%d.%d.%d.%d-%x%s",
-                               coreprefix, &pkt.data[0],
-                               (current_ip & 0xff000000) >> 24,
-                               (current_ip & 0xff0000) >> 16,
-                               (current_ip & 0xff00) >> 8,
-                               (current_ip & 0xff),
-                               (unsigned int) (abstime & 0xffffffff),
-                               coresuffix);
+               snprintf(corename_str,
+                   sizeof(corename_str),
+                   "%s-%s-%d.%d.%d.%d-%x%s",
+                   coreprefix, &pkt.data[0],
+                   (current_ip & 0xff000000) >> 24,
+                   (current_ip & 0xff0000) >> 16,
+                   (current_ip & 0xff00) >> 8,
+                   (current_ip & 0xff),
+                   (unsigned int) (abstime & 0xffffffff),
+                   coresuffix);
        }
 
        if (0 == inet_aton(panicd_ip_str, (struct kdp_in_addr *) &panic_server_ip)) {
@@ -1944,7 +2014,9 @@ kdp_panic_dump(void)
                kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str);
                destination_mac = temp_mac;
        } else {
-               if (!flag_panic_dump_in_progress) goto panic_dump_exit;
+               if (!flag_panic_dump_in_progress) {
+                       goto panic_dump_exit;
+               }
                if (router_specified) {
                        if (0 == inet_aton(router_ip_str, (struct kdp_in_addr *) &parsed_router_ip)) {
                                kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str);
@@ -1958,7 +2030,9 @@ kdp_panic_dump(void)
                }
        }
 
-       if (!flag_panic_dump_in_progress) goto panic_dump_exit;
+       if (!flag_panic_dump_in_progress) {
+               goto panic_dump_exit;
+       }
 
        kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
            destination_mac.ether_addr_octet[0] & 0xff,
@@ -1969,34 +2043,34 @@ kdp_panic_dump(void)
            destination_mac.ether_addr_octet[5] & 0xff);
 
        kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map));
-       kdb_printf("Sending write request for %s\n", corename_str);  
+       kdb_printf("Sending write request for %s\n", corename_str);
 
-       if ((panic_error = kdp_send_crashdump_pkt(KDP_WRQ, corename_str, 0 , NULL)) < 0) {
-               kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
+       if ((panic_error = kdp_send_crashdump_pkt(KDP_WRQ, corename_str, 0, NULL)) < 0) {
+               kdb_printf("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
                goto panic_dump_exit;
        }
 
        /* Just the panic log requested */
        if ((debugger_panic_str != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP)) {
                kdb_printf_unbuffered("Transmitting panic log, please wait: ");
-               kdp_send_crashdump_data(KDP_DATA, corename_str, 
-                                       debug_buf_ptr - debug_buf_base,
-                                       debug_buf_base);
-               kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0));
+               kdp_send_crashdump_data(KDP_DATA, corename_str,
+                   debug_buf_ptr - debug_buf_base,
+                   debug_buf_base);
+               kdp_send_crashdump_pkt(KDP_EOF, NULL, 0, ((void *) 0));
                printf("Please file a bug report on this panic, if possible.\n");
                goto panic_dump_exit;
        }
-  
+
        /* maybe we wanted the systemlog */
-        if (kdp_flag & SYSTEM_LOG_DUMP) {
+       if (kdp_flag & SYSTEM_LOG_DUMP) {
                long start_off = msgbufp->msg_bufx;
-                long len;
+               long len;
 
                kdb_printf_unbuffered("Transmitting system log, please wait: ");
                if (start_off >= msgbufp->msg_bufr) {
                        len = msgbufp->msg_size - start_off;
-                       kdp_send_crashdump_data(KDP_DATA, corename_str, len, 
-                                               msgbufp->msg_bufc + start_off);
+                       kdp_send_crashdump_data(KDP_DATA, corename_str, len,
+                           msgbufp->msg_bufc + start_off);
                        /* seek to remove trailing bytes */
                        kdp_send_crashdump_seek(corename_str, len);
                        start_off  = 0;
@@ -2005,12 +2079,12 @@ kdp_panic_dump(void)
                if (start_off != msgbufp->msg_bufr) {
                        len = msgbufp->msg_bufr - start_off;
                        kdp_send_crashdump_data(KDP_DATA, corename_str, len,
-                                               msgbufp->msg_bufc + start_off);
+                           msgbufp->msg_bufc + start_off);
                }
 
-               kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0));
+               kdp_send_crashdump_pkt(KDP_EOF, NULL, 0, ((void *) 0));
                goto panic_dump_exit;
-        }
+       }
 
        /* We want a core dump if we're here */
        kern_dump(KERN_DUMP_NET);
@@ -2027,7 +2101,7 @@ begin_panic_transfer(void)
        flag_panic_dump_in_progress = TRUE;
 }
 
-void 
+void
 abort_panic_transfer(void)
 {
        flag_panic_dump_in_progress = FALSE;
@@ -2046,21 +2120,21 @@ kdp_serial_send(void *rpkt, unsigned int rpkt_len)
        kdp_serialize_packet((unsigned char *)rpkt, rpkt_len, pal_serial_putc_nocr);
 }
 
-static void 
+static void
 kdp_serial_receive(void *rpkt, unsigned int *rpkt_len, unsigned int timeout)
 {
        int readkar;
        uint64_t now, deadline;
-       
+
        clock_interval_to_deadline(timeout, 1000 * 1000 /* milliseconds */, &deadline);
 
 //     printf("rx\n");
-       for(clock_get_uptime(&now); now < deadline; clock_get_uptime(&now)) {
+       for (clock_get_uptime(&now); now < deadline; clock_get_uptime(&now)) {
                readkar = pal_serial_getc();
-               if(readkar >= 0) {
+               if (readkar >= 0) {
                        unsigned char *packet;
                        //                      printf("got char %02x\n", readkar);
-                       if((packet = kdp_unserialize_packet(readkar,rpkt_len))) {
+                       if ((packet = kdp_unserialize_packet(readkar, rpkt_len))) {
                                memcpy(rpkt, packet, *rpkt_len);
                                return;
                        }
@@ -2072,11 +2146,13 @@ kdp_serial_receive(void *rpkt, unsigned int *rpkt_len, unsigned int timeout)
 static boolean_t
 kdp_serial_setmode(boolean_t active)
 {
-       if (active == FALSE) /* leaving KDP */
+       if (active == FALSE) /* leaving KDP */
                return TRUE;
+       }
 
-       if (!needs_serial_init)
+       if (!needs_serial_init) {
                return TRUE;
+       }
 
        pal_serial_init();
        needs_serial_init = FALSE;
@@ -2084,7 +2160,7 @@ kdp_serial_setmode(boolean_t active)
 }
 
 
-static void 
+static void
 kdp_serial_callout(__unused void *arg, kdp_event_t event)
 {
        /*
@@ -2098,14 +2174,13 @@ kdp_serial_callout(__unused void *arg, kdp_event_t event)
         * reinitialization.
         */
 
-       switch (event)
-       {
-               case KDP_EVENT_PANICLOG:
-               case KDP_EVENT_ENTER:
-                       break;
-               case KDP_EVENT_EXIT:
-                       needs_serial_init = TRUE;
-                       break;
+       switch (event) {
+       case KDP_EVENT_PANICLOG:
+       case KDP_EVENT_ENTER:
+               break;
+       case KDP_EVENT_EXIT:
+               needs_serial_init = TRUE;
+               break;
        }
 }
 
@@ -2131,16 +2206,17 @@ kdp_init(void)
 
 #if defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
        if (vm_kernel_slide) {
-               char    KASLR_stext[19];
+               char    KASLR_stext[19];
                strlcat(kdp_kernelversion_string, "; stext=", sizeof(kdp_kernelversion_string));
                snprintf(KASLR_stext, sizeof(KASLR_stext), "%p", (void *) vm_kernel_stext);
                strlcat(kdp_kernelversion_string, KASLR_stext, sizeof(kdp_kernelversion_string));
        }
 #endif
 
-       if (debug_boot_arg & DB_REBOOT_POST_CORE)
+       if (debug_boot_arg & DB_REBOOT_POST_CORE) {
                kdp_flag |= REBOOT_POST_CORE;
-#if    defined(__x86_64__)     
+       }
+#if     defined(__x86_64__)
        kdp_machine_init();
 #endif
 
@@ -2157,13 +2233,15 @@ kdp_init(void)
        boolean_t kdp_not_serial = kdp_match_name_found ? (strncmp(kdpname, "serial", sizeof(kdpname))) : TRUE;
 
 #if CONFIG_EMBEDDED
-       //respect any custom debugger boot-args
-       if(kdp_match_name_found && kdp_not_serial)
+       //respect any custom debugger boot-args
+       if (kdp_match_name_found && kdp_not_serial) {
                return;
+       }
 #else /* CONFIG_EMBEDDED */
-        // serial must be explicitly requested
-        if(!kdp_match_name_found || kdp_not_serial)
+       // serial must be explicitly requested
+       if (!kdp_match_name_found || kdp_not_serial) {
                return;
+       }
 #endif /* CONFIG_EMBEDDED */
 
 #if CONFIG_EMBEDDED
@@ -2177,9 +2255,9 @@ kdp_init(void)
        kprintf("Initializing serial KDP\n");
 
        kdp_register_callout(kdp_serial_callout, NULL);
-        kdp_register_link(NULL, kdp_serial_setmode);
+       kdp_register_link(NULL, kdp_serial_setmode);
        kdp_register_send_receive(kdp_serial_send, kdp_serial_receive);
-       
+
        /* fake up an ip and mac for early serial debugging */
        macaddr.ether_addr_octet[0] = 's';
        macaddr.ether_addr_octet[1] = 'e';
@@ -2189,13 +2267,13 @@ kdp_init(void)
        macaddr.ether_addr_octet[5] = 'l';
        ipaddr.s_addr = KDP_SERIAL_IPADDR;
        kdp_set_ip_and_mac_addresses(&ipaddr, &macaddr);
-        
+
 #endif /* CONFIG_SERIAL_KDP */
 }
 
 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
 void
-kdp_init(void) 
+kdp_init(void)
 {
 }
 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
@@ -2215,21 +2293,25 @@ uint32_t kdp_stack_snapshot_bytes_traced(void);
 
 void
 kdp_register_send_receive(__unused void *send, __unused void *receive)
-{}
+{
+}
 
 void
 kdp_unregister_send_receive(__unused void *send, __unused void *receive)
-{}
+{
+}
 
 void *
 kdp_get_interface( void)
 {
-        return(void *)0;
+       return (void *)0;
 }
 
 unsigned int
 kdp_get_ip_address(void )
-{ return 0; }
+{
+       return 0;
+}
 
 struct kdp_ether_addr
 kdp_get_mac_addr(void)
@@ -2239,23 +2321,30 @@ kdp_get_mac_addr(void)
 
 void
 kdp_set_ip_and_mac_addresses(
-        __unused struct kdp_in_addr          *ipaddr,
-        __unused struct kdp_ether_addr       *macaddr)
-{}
+       __unused struct kdp_in_addr          *ipaddr,
+       __unused struct kdp_ether_addr       *macaddr)
+{
+}
 
 void
 kdp_set_gateway_mac(__unused void *gatewaymac)
-{}
+{
+}
 
 void
 kdp_set_interface(__unused void *ifp)
-{}
+{
+}
 
-void kdp_register_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
-{}
+void
+kdp_register_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
+{
+}
 
-void kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
-{}
+void
+kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
+{
+}
 
 #endif /* !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING) */
 
@@ -2265,19 +2354,19 @@ extern __attribute__((noreturn)) void panic_spin_forever(void);
 __attribute__((noreturn))
 void
 kdp_raise_exception(
-               __unused unsigned int           exception,
-               __unused unsigned int           code,
-               __unused unsigned int           subcode,
-               __unused void                   *saved_state
-               )
+       __unused unsigned int           exception,
+       __unused unsigned int           code,
+       __unused unsigned int           subcode,
+       __unused void                   *saved_state
+       )
 #else
 void
 kdp_raise_exception(
-               unsigned int            exception,
-               unsigned int            code,
-               unsigned int            subcode,
-               void                    *saved_state
-               )
+       unsigned int            exception,
+       unsigned int            code,
+       unsigned int            subcode,
+       void                    *saved_state
+       )
 #endif
 {
 #if CONFIG_EMBEDDED