remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
{
unsigned char *buf;
- int max_buf_size; /* Max size of packet output buffer */
unsigned char *p;
unsigned char *plen;
long sizeof_buf;
int plenlen;
int todo;
int nr_bytes;
+ int payload_size;
+ unsigned char *payload_start;
- /* Verify that the target can support a binary download */
+ /* Verify that the target can support a binary download. */
check_binary_download (memaddr);
- /* Determine the max packet size. */
- max_buf_size = get_memory_write_packet_size ();
- sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
+ /* Compute the size, and then allocate space for the largest
+ possible packet. Include space for an extra trailing NUL. */
+ sizeof_buf = get_memory_write_packet_size () + 1;
buf = alloca (sizeof_buf);
- /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
- max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
+ /* Compute the size of the actual payload by subtracting out the
+ packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
+ payload_size = (get_memory_write_packet_size () - (strlen ("$M,:#NN")
+ + hexnumlen (memaddr)
+ + hexnumlen (len)));
- /* construct "M"<memaddr>","<len>":" */
- /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
- p = buf;
+ /* Construct the packet header: "[MX]<memaddr>,<len>:". */
- /* Append [XM]. Compute a best guess of the number of bytes
+ /* Append "[XM]". Compute a best guess of the number of bytes
actually transfered. */
+ p = buf;
switch (remote_protocol_binary_download.support)
{
case PACKET_ENABLE:
*p++ = 'X';
/* Best guess at number of bytes that will fit. */
- todo = min (len, max_buf_size);
+ todo = min (len, payload_size);
break;
case PACKET_DISABLE:
*p++ = 'M';
/* num bytes that will fit */
- todo = min (len, max_buf_size / 2);
+ todo = min (len, payload_size / 2);
break;
case PACKET_SUPPORT_UNKNOWN:
internal_error (__FILE__, __LINE__,
internal_error (__FILE__, __LINE__, "bad switch");
}
- /* Append <memaddr> */
+ /* Append "<memaddr>". */
memaddr = remote_address_masked (memaddr);
p += hexnumstr (p, (ULONGEST) memaddr);
+
+ /* Append ",". */
*p++ = ',';
- /* Append <len>. Retain the location/size of <len>. It may
- need to be adjusted once the packet body has been created. */
+ /* Append <len>. Retain the location/size of <len>. It may need to
+ be adjusted once the packet body has been created. */
plen = p;
plenlen = hexnumstr (p, (ULONGEST) todo);
p += plenlen;
+
+ /* Append ":". */
*p++ = ':';
*p = '\0';
- /* Append the packet body. */
+ /* Append the packet body. */
+ payload_start = p;
switch (remote_protocol_binary_download.support)
{
case PACKET_ENABLE:
increasing byte addresses. Only escape certain critical
characters. */
for (nr_bytes = 0;
- (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
+ (nr_bytes < todo) && (p - payload_start) < payload_size;
nr_bytes++)
{
switch (myaddr[nr_bytes] & 0xff)
and we have actually sent fewer bytes than planned.
Fix-up the length field of the packet. Use the same
number of characters as before. */
-
plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
*plen = ':'; /* overwrite \0 from hexnumnstr() */
}