Linux device Drivers Learn notes on the first day (how to run the system on the Development Board, drive the development basic steps) __linux

Source: Internet
Author: User

How to run the system on the Development Board.
4.0 acquisition of the Cross compiler. Manufacturers to provide online download (manufacturer confirmation)
4.1 Uboot to operate.
1, extract the source of the factory
2, enter the source code
3,make Distclean completely delete the source of the target, temporary files
4,make Xxx_config is configured for a CPU and Development Board
5,make compilation
6, Results generated U-boot.bin
7, using the method of burning writing provided by the manufacturer for burning and writing
8,uboot source/include/configs/xxx.h, hardware-Related header file information, is also a key concern for porting files to understand when there is uboot current support for hardware information.

4.2 Kernel to operate

1, extract the source of the factory
2, enter the source code
3,make Distclean completely delete the source of the target, temporary files, get the cleanest source code, do not let the last compilation results affect this compilation
4,make Xxx_defconfig is configured for a CPU and Development Board, Xxx_defconfig is located under the arch/arm/configs/directory to find a profile with the same name.
5,make Menuconfig do three checks:
Check whether the current kernel supports the current CPU architecture;
Checks whether the current kernel supports the current processor;
The System type (processor)-> ....
Check whether the current kernel supports the current development Board;
Board Selection (Development Board)
6,make Zimage/make Uimage
7, compile the result; Arch/arm/boot/zimage (uimage)
8, platform code file arch/arm/mach-processor name/mach-Development Board name. C
Look at this directory under the corresponding platform of the file, the file here a lot of hardware-related;

4.3 Hook up rootfs (root file System)

Method One: can use the manufacturer to provide (the factory provides the root file system is very huge, inside may have many we cannot use the service);
Method Two: Encourage oneself to make rootfs, use BusyBox;

System start Time: Uboot time + kernel start time + hook up root file system time;

4.4 Setting the system startup parameters;
The most critical parameters:
Bootcmd: For loading and booting the kernel
Bootargs: Used to pass parameters to the kernel, indicating that the kernel will hook up the root file system in the future.

The method that is passed to the kernel. Remember
Method One: Using the Bootargs of Uboot,
Method One: If some boardloader have no Bootargs, can pass the parameter in the kernel itself (inside the source code);
To execute in source code:
Make Menuconfig
Boot Options->
(console = XXX) The Default kernel command string//kernel itself passes parameters to itself, moves the cursor to this position, and presses ENTER to modify it, such as starting with an NFS network: root =/dev/nfs Nfsroot = 192.1 68.1.5 can be saved
[] Alwasy Use the default Dernel Commad string//If the Bootargs of *,uboot is invalid, the kernel is delivered effectively;

4.5 NFS Network File system startup considerations
1,ubuntu System must build NFS Network Service
Vim/etc/exports
Sudo/etc/init.d/nfs-kernel-server restart
2, configuring the Kernel to support NFS network file systems
Make Menuconfig
File system->
[*] Network File systems->
[*] Root file System on NFS///must be selected, otherwise the NFS network filesystem cannot be hooked up

Linux device driver Development related content:

Arm&linux working mode.
SVC (Management) mode, USR mode;
usr mode switches to svc mode. switching through software interrupts;
When the code is running in USR mode, the software corresponds to the user space;
When the code is running in SVC mode, the software runs in the kernel space.

User space and kernel space:
User space:
Included software: application (software), C library, its own encapsulation of the production of dynamic library
CPU mode of operation: USR
Virtual address space range: 0x000,0000~ 0xbfff,ffff (3G memory)
The use of virtual memory technology presupposes that the CPU must be integrated MMU
The user cannot access the kernel address space, including code and data;
Users can not directly manipulate hardware resources;
System calls must be used to communicate with kernel space
To communicate with the kernel space is actually the switch of CPU working mode
usr mode switching with svc mode with soft interrupt
Kernel space:
Included software: Process management, memory management, device drivers, file systems, TCP/IP network protocol stacks
Working mode of CPU: SVC
The virtual address space range is: 0xc000,0000 ~ 0xfff,ffff (kernel 1G virtual address space for all processes to share)
Ability to access hardware resources directly

Summary: The purpose of dividing user space and kernel space: to realize the security of operating system
The basis of dividing user space and kernel space depends on ARM working mode
Data communication between user space and kernel space must be based on system call

User Software Programming format

#?include<stdio.h>//Standard C header file
int main (int argc,char *argv[])/Program entry
{
   printf ("Hello word! \ n ");
   Return 0;//program Exit
}

Kernel space Software programming format

#include <linux/module.h>
#include <linux/init.h>//header file is located in kernel source

static int fifth_drv_init (void) {
    printk ("Hello word,%s \n!", __func__);//where k represents kernel return
    0;
}
static void Fifth_drv_exit (void)
{
}
module_init (fifth_drv_init);//module_init into
module_exit ( Fifth_drv_exit);//module_exit out

Entry: Functions modified by Module_init
Exit: function Modified by module_exit
Module_init definition is also located in the kernel source code;

Experiment:

1,mkdir/opt/drivers/day01/1.0-  P     //  p,--parents     create an upper directory when required, as long as the directory exists, not as Error
2, CD   / opt/drivers/day01/1.0 
3, Vim Helloword
4, save exit

Kernel BASIC Programming Specification:
1, do not allow the use of standard C library and header files, the kernel used by the header files are located in the kernel source code
2, the kernel printing function is not printf, but instead uses the print function provided by the kernel PRINTK (); the printout is in the same format as C
3, the kernel module code does not have a main function, replaced by the MODULE_INIT macro specified function as the Kernel module code entry function, the entire software at runtime, the first run is the entry function.
return value of entry function: successfully returned 0 failure returns negative
4, the kernel module code corresponding to the exit is the MODULE_EXIT macro-specified function;
5,module_init, Module_exit Source code is also located in the kernel source
6, kernel-driven programming must pay attention to the legal access to memory, otherwise it will cause system crashes
7, kernel-driven programming does not allow you to handle float or double
8, the process in the user space stack and kernel space stack is not the same, kernel space allocation 8K
9, when compiling kernel driver, need to write additional makefile, to associate source code.
10, the driver corresponding executable file is Ko end, related operation command
Insmod Rmmod Lsmmod Modinfo
11, the Kernel Module information License statement must be added: Module_license ("GPL");
12, the module command line to pass the parameter
Essential purpose: To pass parameter information to a module while loading a module or module run (before unloading the module)

Written questions: Describe your understanding of static.

Kernel Module Learning source code compilation problem:
Clear: Kernel module source code compilation must be combined with the kernel source/opt/kernel
The compilation simply adds a makefile to the/opt/drivers/day01/1.0:

Obj-m + + HELLOWORD.O #将helloword. C compiled into the corresponding binary executable module file Helloword.ko
kdir =/opt/kernel  #指定内核源码路径 all
:  # Pseudo target, here is not dependent on
    #make前是tab键而非空格
    make-c $ (kdir) Subdirs = $ (PWD) Modules 
    #-C indicates that a specified directory is compiled, similar to: Cd/opt/kernel;
    Make # Subdirs: Specifies a subdirectory, which is the current path. Where PWD is the system global environment variable, the current directory; Tell the kernel source code, the file below this directory needs to be compiled into a module
    #采用模块化编译, the. C is compiled into the. ko file
    #展开为: make-c/opt/kernel subdirs=/opt/ drivers/day01/1.0 modules Clean

:
    make-c $ (kdir) Subdirs = $ (PWD) Clean

Compile:
Just execute the Make command
Result HELLOWORLD.C compiled into the corresponding Helloworld.ko file
Note: Before compiling the HELLOWORLD.C, if the kernel source code is not configured, compiled, it must be configured, compiled

Debug Macros:
FILE,FUNCTION,line,DATE, time

Vim tips:
If there is a duplicate function or variable in the same file, you can CTRL + N automatically fill it up
Row replication: Shift+v Select rows, and then select them up and down

How do I run a kernel module executable file Helloword.ko? Kernel space is actually our zimage;

Insmod Helloword.ko #加载模块到内核中, once the load is successful, the kernel first executes the corresponding entry function, if the entry function returns 0, the module is loaded successfully, the module is formally put into operation, and if a negative number is returned, the module fails to load
rmmod Helloword  #用于从内核中卸载模块, when the module is unloaded, the corresponding export function is executed by the kernel, once unloaded, the kernel has no such module
lsmod  #查看当前内核有哪些加载的模块
modinfo Helloword # View module. Ko's own properties or attributes

Experiment steps:

1,file Helloworld.ko View the properties of the module
 1,CP helloworld.ko  /opt/rootfs///   Copy to the Development Board root file system
 2,insmod Helloworld.ko
 4,lsmod
 

Problems that may be encountered
Question 1:
Helloworld:module license ' unspecified ' taints kernel.
Reason: The HelloWorld module does not specify a specific license statement.
Workaround: Add module_license ("GPL"); Add license information must be added

Question 2:
When you uninstall the module, you are prompted for the following information, unable to uninstall: Rmmod chdir (/lib/modules) No such file ....
Reason: No directory
Workaround:
Executing in the Development Board: Mkdir/lib/modules
If prompted: Rmmod chdir (/lib/modules/2.6.3.5) No such file ....
Executing in the Development Board: mkdir/lib/modules/2.6.3.5

Kernel module parameters

Command-line arguments for the application:

int main (int argc,char *argv[]) {//ARGC represents the number of parameters, argv parameter information
      int a,b,c;
      if (argc<4) {
         return  -1;
      }
       Converts a character to an integer
      a = Strtoul (argv[1],null,0);
      b = Strtoul (argv[2],null,0);
      c = Strtoul (argv[3],null,0);
      printf ("A =%d,b=%b,c=%c", a,b,c);
}

Run./a.out 100 200 300

Q: The kernel module source corresponding to the Ko file, when loading, can be like the application of the command line to pass the parameters as well. You can, you need to take advantage of the kernel's module parameters.
Use of kernel modules:
Objective: To be able to pass additional parameter information to the module during the loading module or module operation (as long as the module is not unloaded)

static int irq;//0 static char *pstr;//null/** * Module_param (name,type,perm) * Function: Specify module parameters, Pass parameters to Module * Name: module parameter's name * Type: Data type of module parameter, not allow floating-point data if must be * bool Inbool charp short ushort int UINT long U LONG * Perm: Access rights for module parameters. Permissions are generally expressed in octal, such as 0664 (0 for octal) * If perm (permission) is not 0, then load the module, the/sys/modules directory will be generated in a directory with the same name of the module name, in the paramter directory of this directory will have the same name as the variable name file, Modify this file to modify the value of the variable * if perm (permission) is 0, the permission file **/Module_param (irq,int,0664) is not generated;//module parameter declaration, 0664 readable writable Module_param (Pstr,char p,0)//module parameter declaration/* * If the permission for the IRQ is 0664, the/sys/modules/helloworld/parameters/variable name is generated and the contents of the file are the contents of the variable * can be written by "Echo >/S  ys/modules/helloworld/parameters/variable Name "Modify variable (will modify file content)/static int fifth_drv_init (void) {PRINTK (" init IRQ =%D,PSTR =
    %s \ n ", irq,pstr);
return 0; } static void Fifth_drv_exit (void) {PRINTK ("exit IRQ =%d,pstr =%s \ n", irq,pstr); Module_init (Fifth_drv_init); Odule_init into Module_exit (fifth_drv_exit);//module_exit out 

Exit the above save, and then copy the file to ... , the experimental steps:

Insmod Helloworld.ko
rmmod helloworld
insmod IRQ = Helloworld.ko  = pstr  #传参
Tarena MODULES/HELLOWORLD/PARAMETERS/IRQ #查看irq文件的内容
echo 255 >/sys/modules/helloworld/parameters/irq #向文件写入新值
rmmod HelloWorld #查看输出的值是否发生了变化

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.