how to access and operate a binarry file ?


In some exception case, we need to check and modified a binary file byte by byte , in a Windows-like OS, we could use some tools like UtralEdit and so on.

but in a Linux/Unix-like OS, how can we do this ?

I suggest VIM.

VIM is a smart and powerful and flexible tool , it can do too much more things than our cognition.

here I'll show you how to use Vim to read and modified a binary file.


first, open the binary file by vim :

       vim  -b  test.bin

    at this this moment,  you'll see some digital gibberish like below:

         QFI<fb>^@^@^@^C^@^@^@^@^@^@^A^H^@^@^@*^@^@^@^P^@^@^@^B@^@^@^@^@^@^@^@^@^@^@^R^@^@^@^@^@^C^@^@^@^@^@^@^@^A^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^    @^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^D^@^@^@hh^C<f8>W^@^@^@<90>^@^@dirty bit^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^Acorrupt bit^@^@    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@lazy refcounts^@^@^@^


...


then, type "%!xxd" in Vim's command mode, you'll see the digital gibberish been converted to hex and showed like below:

    1 0000000: 5146 49fb 0000 0003 0000 0000 0000 0108  QFI.............
    2 0000010: 0000 002a 0000 0010 0000 0002 4000 0000  ...*........@...
    3 0000020: 0000 0000 0000 0012 0000 0000 0003 0000  ................
    4 0000030: 0000 0000 0001 0000 0000 0001 0000 0000  ................
    5 0000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    6 0000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    7 0000060: 0000 0004 0000 0068 6803 f857 0000 0090  .......hh..W....
    8 0000070: 0000 6469 7274 7920 6269 7400 0000 0000  ..dirty bit.....
    9 0000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   10 0000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   11 00000a0: 0001 636f 7272 7570 7420 6269 7400 0000  ..corrupt bit...
   12 00000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   13 00000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   14 00000d0: 0100 6c61 7a79 2072 6566 636f 756e 7473  ..lazy refcounts
   15 00000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   16 00000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   17 0000100: 0000 0000 0000 0000 2f72 6f6f 742f 7465  ......../root/te
   18 0000110: 7374 2f69 6d61 6765 2f69 6133 3265 5f72  st/image/ia32e_r
   19 0000120: 6865 6c37 7532 5f63 7075 3230 3036 2e69  hel7u2_cpu2006.i
   20 0000130: 6d67 0000 0000 0000 0000 0000 0000 0000  mg..............


now, you can modify the hex words in the Vim's edit mode, for example , here I will modify the red block of the above text.

after it , type "%!xxd   -r" to convert the hex type to binary type,

save this file like you always doing in Vim. Now , this binary file had been modified .


open this file again , you'll see like below:

    1 0000000: 0000 1400 0000 0000 0000 0000 0000 0000  ................
    2 0000010: 0000 002a 0000 0010 0000 0002 4000 0000  ...*........@...
    3 0000020: 0000 0000 0000 0012 0000 0000 0003 0000  ................
    4 0000030: 0000 0000 0001 0000 0000 0001 0000 0000  ................
    5 0000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    6 0000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    7 0000060: 0000 0004 0000 0068 6803 f857 0000 0090  .......hh..W....
    8 0000070: 0000 6469 7274 7920 6269 7400 0000 0000  ..dirty bit.....
    9 0000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   10 0000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   11 00000a0: 0001 636f 7272 7570 7420 6269 7400 0000  ..corrupt bit...
   12 00000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   13 00000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   14 00000d0: 0100 6c61 7a79 2072 6566 636f 756e 7473  ..lazy refcounts
   15 00000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   16 00000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   17 0000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   18 0000110: 7374 2f69 6d61 6765 2f69 6133 3265 5f72  st/image/ia32e_r
   19 0000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   20 0000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   21 0000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   22 0000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................


see the red block , what difference you've found than before?

good job !


haha, thanks.



### DWC3 Endpoint Configuration and Usage in USB Driver Development In USB driver development, configuring endpoints (EPs) is crucial for establishing communication between a device and host. For the DesignWare Core USB 3.0 (DWC_usb3 or DWC3), each endpoint has specific attributes that must be set up correctly to ensure proper data transfer. Endpoints within DWC3 are bidirectional entities but operate unidirectionally at any given time; they can either send or receive data depending on their direction setting[^1]. When initializing an endpoint, several parameters need attention: - **Direction**: Determines whether EP will act as IN (device-to-host) or OUT (host-to-device). - **Type**: Specifies if it’s Control, Isochronous, Bulk, or Interrupt. - **Maximum Packet Size (MPS)**: Defines maximum size of packets this EP handles during transfers. For configuration purposes, developers often interact with structures like `struct dwc3_ep` which encapsulates all necessary details about an individual endpoint including its capabilities and current state. This structure also contains pointers to DMA buffers used by hardware when performing actual I/O operations. To manage these configurations programmatically, one typically uses functions provided by the DWC3 core layer such as `dwc3_endpoint_config()` where appropriate settings from user space get applied directly into corresponding registers via register access methods implemented inside drivers/usb/dwc3/core.c file[^2]. Additionally, managing queues associated with endpoints involves using linked lists similar to those found while working with other types of devices utilizing generic Linux kernel APIs. Specifically, `struct list_head` plays a significant role here allowing efficient addition/removal of requests without disrupting ongoing transactions significantly impacting performance positively especially under heavy load conditions. ```c // Example C code snippet showing how to configure an endpoint void example_configure_dwc3_endpoint(struct dwc3 *dwc3, struct dwc3_ep *dep) { u8 ep_type; /* Set endpoint type */ dep->endpoint.type = ep_type; /* Configure Maximum Packet Size based on requirements */ dwc3_set_maximum_packet_size(dwc3, dep); /* Apply changes through low-level interface */ dwc3_gadget_writel(dwc3, DGCMD_SET_CONFIG_EP, &config); } ``` --related questions-- 1. How does the DWC3 handle different types of USB transfers? 2. What mechanisms exist for error handling in DWC3 endpoint communications? 3. Can you explain more about the role of DMA buffers in DWC3 operation? 4. In what scenarios would adjusting the MPS value impact system performance?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值