xref: /OK3568_Linux_fs/yocto/poky/documentation/kernel-dev/common.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: CC-BY-SA-2.0-UK
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun************
4*4882a593SmuzhiyunCommon Tasks
5*4882a593Smuzhiyun************
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThis chapter presents several common tasks you perform when you work
8*4882a593Smuzhiyunwith the Yocto Project Linux kernel. These tasks include preparing your
9*4882a593Smuzhiyunhost development system for kernel development, preparing a layer,
10*4882a593Smuzhiyunmodifying an existing recipe, patching the kernel, configuring the
11*4882a593Smuzhiyunkernel, iterative development, working with your own sources, and
12*4882a593Smuzhiyunincorporating out-of-tree modules.
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun.. note::
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun   The examples presented in this chapter work with the Yocto Project
17*4882a593Smuzhiyun   2.4 Release and forward.
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunPreparing the Build Host to Work on the Kernel
20*4882a593Smuzhiyun==============================================
21*4882a593Smuzhiyun
22*4882a593SmuzhiyunBefore you can do any kernel development, you need to be sure your build
23*4882a593Smuzhiyunhost is set up to use the Yocto Project. For information on how to get
24*4882a593Smuzhiyunset up, see the ":doc:`/dev-manual/start`" section in
25*4882a593Smuzhiyunthe Yocto Project Development Tasks Manual. Part of preparing the system
26*4882a593Smuzhiyunis creating a local Git repository of the
27*4882a593Smuzhiyun:term:`Source Directory` (``poky``) on your system. Follow the steps in the
28*4882a593Smuzhiyun":ref:`dev-manual/start:cloning the \`\`poky\`\` repository`"
29*4882a593Smuzhiyunsection in the Yocto Project Development Tasks Manual to set up your
30*4882a593SmuzhiyunSource Directory.
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun.. note::
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun   Be sure you check out the appropriate development branch or you
35*4882a593Smuzhiyun   create your local branch by checking out a specific tag to get the
36*4882a593Smuzhiyun   desired version of Yocto Project. See the
37*4882a593Smuzhiyun   ":ref:`dev-manual/start:checking out by branch in poky`" and
38*4882a593Smuzhiyun   ":ref:`dev-manual/start:checking out by tag in poky`"
39*4882a593Smuzhiyun   sections in the Yocto Project Development Tasks Manual for more information.
40*4882a593Smuzhiyun
41*4882a593SmuzhiyunKernel development is best accomplished using
42*4882a593Smuzhiyun:ref:`devtool <sdk-manual/extensible:using \`\`devtool\`\` in your sdk workflow>`
43*4882a593Smuzhiyunand not through traditional kernel workflow methods. The remainder of
44*4882a593Smuzhiyunthis section provides information for both scenarios.
45*4882a593Smuzhiyun
46*4882a593SmuzhiyunGetting Ready to Develop Using ``devtool``
47*4882a593Smuzhiyun------------------------------------------
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunFollow these steps to prepare to update the kernel image using
50*4882a593Smuzhiyun``devtool``. Completing this procedure leaves you with a clean kernel
51*4882a593Smuzhiyunimage and ready to make modifications as described in the
52*4882a593Smuzhiyun":ref:`kernel-dev/common:using \`\`devtool\`\` to patch the kernel`"
53*4882a593Smuzhiyunsection:
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun1. *Initialize the BitBake Environment:* Before building an extensible
56*4882a593Smuzhiyun   SDK, you need to initialize the BitBake build environment by sourcing
57*4882a593Smuzhiyun   the build environment script (i.e. :ref:`structure-core-script`)::
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun      $ cd poky
60*4882a593Smuzhiyun      $ source oe-init-build-env
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun   .. note::
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun      The previous commands assume the
65*4882a593Smuzhiyun      :ref:`overview-manual/development-environment:yocto project source repositories`
66*4882a593Smuzhiyun      (i.e. ``poky``) have been cloned using Git and the local repository is named
67*4882a593Smuzhiyun      "poky".
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun2. *Prepare Your local.conf File:* By default, the
70*4882a593Smuzhiyun   :term:`MACHINE` variable is set to
71*4882a593Smuzhiyun   "qemux86-64", which is fine if you are building for the QEMU emulator
72*4882a593Smuzhiyun   in 64-bit mode. However, if you are not, you need to set the
73*4882a593Smuzhiyun   :term:`MACHINE` variable appropriately in your ``conf/local.conf`` file
74*4882a593Smuzhiyun   found in the
75*4882a593Smuzhiyun   :term:`Build Directory` (i.e.
76*4882a593Smuzhiyun   ``poky/build`` in this example).
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun   Also, since you are preparing to work on the kernel image, you need
79*4882a593Smuzhiyun   to set the
80*4882a593Smuzhiyun   :term:`MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS`
81*4882a593Smuzhiyun   variable to include kernel modules.
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun   In this example we wish to build for qemux86 so we must set the
84*4882a593Smuzhiyun   :term:`MACHINE` variable to "qemux86" and also add the "kernel-modules".
85*4882a593Smuzhiyun   As described we do this by appending to ``conf/local.conf``::
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun      MACHINE = "qemux86"
88*4882a593Smuzhiyun      MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS += "kernel-modules"
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun3. *Create a Layer for Patches:* You need to create a layer to hold
91*4882a593Smuzhiyun   patches created for the kernel image. You can use the
92*4882a593Smuzhiyun   ``bitbake-layers create-layer`` command as follows::
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun      $ cd poky/build
95*4882a593Smuzhiyun      $ bitbake-layers create-layer ../../meta-mylayer
96*4882a593Smuzhiyun      NOTE: Starting bitbake server...
97*4882a593Smuzhiyun      Add your new layer with 'bitbake-layers add-layer ../../meta-mylayer'
98*4882a593Smuzhiyun      $
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun   .. note::
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun      For background information on working with common and BSP layers,
103*4882a593Smuzhiyun      see the
104*4882a593Smuzhiyun      ":ref:`dev-manual/common-tasks:understanding and creating layers`"
105*4882a593Smuzhiyun      section in the Yocto Project Development Tasks Manual and the
106*4882a593Smuzhiyun      ":ref:`bsp-guide/bsp:bsp layers`" section in the Yocto Project Board
107*4882a593Smuzhiyun      Support (BSP) Developer's Guide, respectively. For information on how to
108*4882a593Smuzhiyun      use the ``bitbake-layers create-layer`` command to quickly set up a layer,
109*4882a593Smuzhiyun      see the
110*4882a593Smuzhiyun      ":ref:`dev-manual/common-tasks:creating a general layer using the \`\`bitbake-layers\`\` script`"
111*4882a593Smuzhiyun      section in the Yocto Project Development Tasks Manual.
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun4. *Inform the BitBake Build Environment About Your Layer:* As directed
114*4882a593Smuzhiyun   when you created your layer, you need to add the layer to the
115*4882a593Smuzhiyun   :term:`BBLAYERS` variable in the
116*4882a593Smuzhiyun   ``bblayers.conf`` file as follows::
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun      $ cd poky/build
119*4882a593Smuzhiyun      $ bitbake-layers add-layer ../../meta-mylayer
120*4882a593Smuzhiyun      NOTE: Starting bitbake server...
121*4882a593Smuzhiyun      $
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun5. *Build the Extensible SDK:* Use BitBake to build the extensible SDK
124*4882a593Smuzhiyun   specifically for use with images to be run using QEMU::
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun      $ cd poky/build
127*4882a593Smuzhiyun      $ bitbake core-image-minimal -c populate_sdk_ext
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun   Once
130*4882a593Smuzhiyun   the build finishes, you can find the SDK installer file (i.e.
131*4882a593Smuzhiyun   ``*.sh`` file) in the following directory::
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun      poky/build/tmp/deploy/sdk
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun   For this example, the installer file is named
136*4882a593Smuzhiyun   ``poky-glibc-x86_64-core-image-minimal-i586-toolchain-ext-&DISTRO;.sh``.
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun6. *Install the Extensible SDK:* Use the following command to install
139*4882a593Smuzhiyun   the SDK. For this example, install the SDK in the default
140*4882a593Smuzhiyun   ``poky_sdk`` directory::
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun      $ cd poky/build/tmp/deploy/sdk
143*4882a593Smuzhiyun      $ ./poky-glibc-x86_64-core-image-minimal-i586-toolchain-ext-&DISTRO;.sh
144*4882a593Smuzhiyun      Poky (Yocto Project Reference Distro) Extensible SDK installer version &DISTRO;
145*4882a593Smuzhiyun      ============================================================================
146*4882a593Smuzhiyun      Enter target directory for SDK (default: poky_sdk):
147*4882a593Smuzhiyun      You are about to install the SDK to "/home/scottrif/poky_sdk". Proceed [Y/n]? Y
148*4882a593Smuzhiyun      Extracting SDK......................................done
149*4882a593Smuzhiyun      Setting it up...
150*4882a593Smuzhiyun      Extracting buildtools...
151*4882a593Smuzhiyun      Preparing build system...
152*4882a593Smuzhiyun      Parsing recipes: 100% |#################################################################| Time: 0:00:52
153*4882a593Smuzhiyun      Initializing tasks: 100% |############## ###############################################| Time: 0:00:04
154*4882a593Smuzhiyun      Checking sstate mirror object availability: 100% |######################################| Time: 0:00:00
155*4882a593Smuzhiyun      Parsing recipes: 100% |#################################################################| Time: 0:00:33
156*4882a593Smuzhiyun      Initializing tasks: 100% |##############################################################| Time: 0:00:00
157*4882a593Smuzhiyun      done
158*4882a593Smuzhiyun      SDK has been successfully set up and is ready to be used.
159*4882a593Smuzhiyun      Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
160*4882a593Smuzhiyun       $ . /home/scottrif/poky_sdk/environment-setup-i586-poky-linux
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun7. *Set Up a New Terminal to Work With the Extensible SDK:* You must set
164*4882a593Smuzhiyun   up a new terminal to work with the SDK. You cannot use the same
165*4882a593Smuzhiyun   BitBake shell used to build the installer.
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun   After opening a new shell, run the SDK environment setup script as
168*4882a593Smuzhiyun   directed by the output from installing the SDK::
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun      $ source poky_sdk/environment-setup-i586-poky-linux
171*4882a593Smuzhiyun      "SDK environment now set up; additionally you may now run devtool to perform development tasks.
172*4882a593Smuzhiyun      Run devtool --help for further details.
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun   .. note::
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun      If you get a warning about attempting to use the extensible SDK in
177*4882a593Smuzhiyun      an environment set up to run BitBake, you did not use a new shell.
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun8. *Build the Clean Image:* The final step in preparing to work on the
180*4882a593Smuzhiyun   kernel is to build an initial image using ``devtool`` in the new
181*4882a593Smuzhiyun   terminal you just set up and initialized for SDK work::
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun      $ devtool build-image
184*4882a593Smuzhiyun      Parsing recipes: 100% |##########################################| Time: 0:00:05
185*4882a593Smuzhiyun      Parsing of 830 .bb files complete (0 cached, 830 parsed). 1299 targets, 47 skipped, 0 masked, 0 errors.
186*4882a593Smuzhiyun      WARNING: No packages to add, building image core-image-minimal unmodified
187*4882a593Smuzhiyun      Loading cache: 100% |############################################| Time: 0:00:00
188*4882a593Smuzhiyun      Loaded 1299 entries from dependency cache.
189*4882a593Smuzhiyun      NOTE: Resolving any missing task queue dependencies
190*4882a593Smuzhiyun      Initializing tasks: 100% |#######################################| Time: 0:00:07
191*4882a593Smuzhiyun      Checking sstate mirror object availability: 100% |###############| Time: 0:00:00
192*4882a593Smuzhiyun      NOTE: Executing SetScene Tasks
193*4882a593Smuzhiyun      NOTE: Executing RunQueue Tasks
194*4882a593Smuzhiyun      NOTE: Tasks Summary: Attempted 2866 tasks of which 2604 didn't need to be rerun and all succeeded.
195*4882a593Smuzhiyun      NOTE: Successfully built core-image-minimal. You can find output files in /home/scottrif/poky_sdk/tmp/deploy/images/qemux86
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun   If you were
198*4882a593Smuzhiyun   building for actual hardware and not for emulation, you could flash
199*4882a593Smuzhiyun   the image to a USB stick on ``/dev/sdd`` and boot your device. For an
200*4882a593Smuzhiyun   example that uses a Minnowboard, see the
201*4882a593Smuzhiyun   :yocto_wiki:`TipsAndTricks/KernelDevelopmentWithEsdk </TipsAndTricks/KernelDevelopmentWithEsdk>`
202*4882a593Smuzhiyun   Wiki page.
203*4882a593Smuzhiyun
204*4882a593SmuzhiyunAt this point you have set up to start making modifications to the
205*4882a593Smuzhiyunkernel by using the extensible SDK. For a continued example, see the
206*4882a593Smuzhiyun":ref:`kernel-dev/common:using \`\`devtool\`\` to patch the kernel`"
207*4882a593Smuzhiyunsection.
208*4882a593Smuzhiyun
209*4882a593SmuzhiyunGetting Ready for Traditional Kernel Development
210*4882a593Smuzhiyun------------------------------------------------
211*4882a593Smuzhiyun
212*4882a593SmuzhiyunGetting ready for traditional kernel development using the Yocto Project
213*4882a593Smuzhiyuninvolves many of the same steps as described in the previous section.
214*4882a593SmuzhiyunHowever, you need to establish a local copy of the kernel source since
215*4882a593Smuzhiyunyou will be editing these files.
216*4882a593Smuzhiyun
217*4882a593SmuzhiyunFollow these steps to prepare to update the kernel image using
218*4882a593Smuzhiyuntraditional kernel development flow with the Yocto Project. Completing
219*4882a593Smuzhiyunthis procedure leaves you ready to make modifications to the kernel
220*4882a593Smuzhiyunsource as described in the ":ref:`kernel-dev/common:using traditional kernel development to patch the kernel`"
221*4882a593Smuzhiyunsection:
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun1. *Initialize the BitBake Environment:* Before you can do anything
224*4882a593Smuzhiyun   using BitBake, you need to initialize the BitBake build environment
225*4882a593Smuzhiyun   by sourcing the build environment script (i.e.
226*4882a593Smuzhiyun   :ref:`structure-core-script`).
227*4882a593Smuzhiyun   Also, for this example, be sure that the local branch you have
228*4882a593Smuzhiyun   checked out for ``poky`` is the Yocto Project &DISTRO_NAME; branch. If
229*4882a593Smuzhiyun   you need to checkout out the &DISTRO_NAME; branch, see the
230*4882a593Smuzhiyun   ":ref:`dev-manual/start:checking out by branch in poky`"
231*4882a593Smuzhiyun   section in the Yocto Project Development Tasks Manual.
232*4882a593Smuzhiyun   ::
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun      $ cd poky
235*4882a593Smuzhiyun      $ git branch
236*4882a593Smuzhiyun      master
237*4882a593Smuzhiyun      * &DISTRO_NAME_NO_CAP;
238*4882a593Smuzhiyun      $ source oe-init-build-env
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun   .. note::
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun      The previous commands assume the
243*4882a593Smuzhiyun      :ref:`overview-manual/development-environment:yocto project source repositories`
244*4882a593Smuzhiyun      (i.e. ``poky``) have been cloned using Git and the local repository is named
245*4882a593Smuzhiyun      "poky".
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun2. *Prepare Your local.conf File:* By default, the
248*4882a593Smuzhiyun   :term:`MACHINE` variable is set to
249*4882a593Smuzhiyun   "qemux86-64", which is fine if you are building for the QEMU emulator
250*4882a593Smuzhiyun   in 64-bit mode. However, if you are not, you need to set the
251*4882a593Smuzhiyun   :term:`MACHINE` variable appropriately in your ``conf/local.conf`` file
252*4882a593Smuzhiyun   found in the
253*4882a593Smuzhiyun   :term:`Build Directory` (i.e.
254*4882a593Smuzhiyun   ``poky/build`` in this example).
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun   Also, since you are preparing to work on the kernel image, you need
257*4882a593Smuzhiyun   to set the
258*4882a593Smuzhiyun   :term:`MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS`
259*4882a593Smuzhiyun   variable to include kernel modules.
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun   In this example we wish to build for qemux86 so we must set the
262*4882a593Smuzhiyun   :term:`MACHINE` variable to "qemux86" and also add the "kernel-modules".
263*4882a593Smuzhiyun   As described we do this by appending to ``conf/local.conf``::
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun      MACHINE = "qemux86"
266*4882a593Smuzhiyun      MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS += "kernel-modules"
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun3. *Create a Layer for Patches:* You need to create a layer to hold
269*4882a593Smuzhiyun   patches created for the kernel image. You can use the
270*4882a593Smuzhiyun   ``bitbake-layers create-layer`` command as follows::
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun      $ cd poky/build
273*4882a593Smuzhiyun      $ bitbake-layers create-layer ../../meta-mylayer
274*4882a593Smuzhiyun      NOTE: Starting bitbake server...
275*4882a593Smuzhiyun      Add your new layer with 'bitbake-layers add-layer ../../meta-mylayer'
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun   .. note::
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun      For background information on working with common and BSP layers,
280*4882a593Smuzhiyun      see the
281*4882a593Smuzhiyun      ":ref:`dev-manual/common-tasks:understanding and creating layers`"
282*4882a593Smuzhiyun      section in the Yocto Project Development Tasks Manual and the
283*4882a593Smuzhiyun      ":ref:`bsp-guide/bsp:bsp layers`" section in the Yocto Project Board
284*4882a593Smuzhiyun      Support (BSP) Developer's Guide, respectively. For information on how to
285*4882a593Smuzhiyun      use the ``bitbake-layers create-layer`` command to quickly set up a layer,
286*4882a593Smuzhiyun      see the
287*4882a593Smuzhiyun      ":ref:`dev-manual/common-tasks:creating a general layer using the \`\`bitbake-layers\`\` script`"
288*4882a593Smuzhiyun      section in the Yocto Project Development Tasks Manual.
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun4. *Inform the BitBake Build Environment About Your Layer:* As directed
291*4882a593Smuzhiyun   when you created your layer, you need to add the layer to the
292*4882a593Smuzhiyun   :term:`BBLAYERS` variable in the
293*4882a593Smuzhiyun   ``bblayers.conf`` file as follows::
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun      $ cd poky/build
296*4882a593Smuzhiyun      $ bitbake-layers add-layer ../../meta-mylayer
297*4882a593Smuzhiyun      NOTE: Starting bitbake server ...
298*4882a593Smuzhiyun      $
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun5. *Create a Local Copy of the Kernel Git Repository:* You can find Git
301*4882a593Smuzhiyun   repositories of supported Yocto Project kernels organized under
302*4882a593Smuzhiyun   "Yocto Linux Kernel" in the Yocto Project Source Repositories at
303*4882a593Smuzhiyun   :yocto_git:`/`.
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun   For simplicity, it is recommended that you create your copy of the
306*4882a593Smuzhiyun   kernel Git repository outside of the
307*4882a593Smuzhiyun   :term:`Source Directory`, which is
308*4882a593Smuzhiyun   usually named ``poky``. Also, be sure you are in the
309*4882a593Smuzhiyun   ``standard/base`` branch.
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun   The following commands show how to create a local copy of the
312*4882a593Smuzhiyun   ``linux-yocto-4.12`` kernel and be in the ``standard/base`` branch.
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun   .. note::
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun      The ``linux-yocto-4.12`` kernel can be used with the Yocto Project 2.4
317*4882a593Smuzhiyun      release and forward.
318*4882a593Smuzhiyun      You cannot use the ``linux-yocto-4.12`` kernel with releases prior to
319*4882a593Smuzhiyun      Yocto Project 2.4.
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun   ::
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun      $ cd ~
324*4882a593Smuzhiyun      $ git clone git://git.yoctoproject.org/linux-yocto-4.12 --branch standard/base
325*4882a593Smuzhiyun      Cloning into 'linux-yocto-4.12'...
326*4882a593Smuzhiyun      remote: Counting objects: 6097195, done.
327*4882a593Smuzhiyun      remote: Compressing objects: 100% (901026/901026), done.
328*4882a593Smuzhiyun      remote: Total 6097195 (delta 5152604), reused 6096847 (delta 5152256)
329*4882a593Smuzhiyun      Receiving objects: 100% (6097195/6097195), 1.24 GiB | 7.81 MiB/s, done.
330*4882a593Smuzhiyun      Resolving deltas: 100% (5152604/5152604), done. Checking connectivity... done.
331*4882a593Smuzhiyun      Checking out files: 100% (59846/59846), done.
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun6. *Create a Local Copy of the Kernel Cache Git Repository:* For
334*4882a593Smuzhiyun   simplicity, it is recommended that you create your copy of the kernel
335*4882a593Smuzhiyun   cache Git repository outside of the
336*4882a593Smuzhiyun   :term:`Source Directory`, which is
337*4882a593Smuzhiyun   usually named ``poky``. Also, for this example, be sure you are in
338*4882a593Smuzhiyun   the ``yocto-4.12`` branch.
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun   The following commands show how to create a local copy of the
341*4882a593Smuzhiyun   ``yocto-kernel-cache`` and switch to the ``yocto-4.12`` branch::
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun      $ cd ~
344*4882a593Smuzhiyun      $ git clone git://git.yoctoproject.org/yocto-kernel-cache --branch yocto-4.12
345*4882a593Smuzhiyun      Cloning into 'yocto-kernel-cache'...
346*4882a593Smuzhiyun      remote: Counting objects: 22639, done.
347*4882a593Smuzhiyun      remote: Compressing objects: 100% (9761/9761), done.
348*4882a593Smuzhiyun      remote: Total 22639 (delta 12400), reused 22586 (delta 12347)
349*4882a593Smuzhiyun      Receiving objects: 100% (22639/22639), 22.34 MiB | 6.27 MiB/s, done.
350*4882a593Smuzhiyun      Resolving deltas: 100% (12400/12400), done.
351*4882a593Smuzhiyun      Checking connectivity... done.
352*4882a593Smuzhiyun
353*4882a593SmuzhiyunAt this point, you are ready to start making modifications to the kernel
354*4882a593Smuzhiyunusing traditional kernel development steps. For a continued example, see
355*4882a593Smuzhiyunthe ":ref:`kernel-dev/common:using traditional kernel development to patch the kernel`"
356*4882a593Smuzhiyunsection.
357*4882a593Smuzhiyun
358*4882a593SmuzhiyunCreating and Preparing a Layer
359*4882a593Smuzhiyun==============================
360*4882a593Smuzhiyun
361*4882a593SmuzhiyunIf you are going to be modifying kernel recipes, it is recommended that
362*4882a593Smuzhiyunyou create and prepare your own layer in which to do your work. Your
363*4882a593Smuzhiyunlayer contains its own :term:`BitBake`
364*4882a593Smuzhiyunappend files (``.bbappend``) and provides a convenient mechanism to
365*4882a593Smuzhiyuncreate your own recipe files (``.bb``) as well as store and use kernel
366*4882a593Smuzhiyunpatch files. For background information on working with layers, see the
367*4882a593Smuzhiyun":ref:`dev-manual/common-tasks:understanding and creating layers`"
368*4882a593Smuzhiyunsection in the Yocto Project Development Tasks Manual.
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun.. note::
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun   The Yocto Project comes with many tools that simplify tasks you need
373*4882a593Smuzhiyun   to perform. One such tool is the ``bitbake-layers create-layer``
374*4882a593Smuzhiyun   command, which simplifies creating a new layer. See the
375*4882a593Smuzhiyun   ":ref:`dev-manual/common-tasks:creating a general layer using the \`\`bitbake-layers\`\` script`"
376*4882a593Smuzhiyun   section in the Yocto Project Development Tasks Manual for
377*4882a593Smuzhiyun   information on how to use this script to quick set up a new layer.
378*4882a593Smuzhiyun
379*4882a593SmuzhiyunTo better understand the layer you create for kernel development, the
380*4882a593Smuzhiyunfollowing section describes how to create a layer without the aid of
381*4882a593Smuzhiyuntools. These steps assume creation of a layer named ``mylayer`` in your
382*4882a593Smuzhiyunhome directory:
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun1. *Create Structure*: Create the layer's structure::
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun      $ mkdir meta-mylayer
387*4882a593Smuzhiyun      $ mkdir meta-mylayer/conf
388*4882a593Smuzhiyun      $ mkdir meta-mylayer/recipes-kernel
389*4882a593Smuzhiyun      $ mkdir meta-mylayer/recipes-kernel/linux
390*4882a593Smuzhiyun      $ mkdir meta-mylayer/recipes-kernel/linux/linux-yocto
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun   The ``conf`` directory holds your configuration files, while the
393*4882a593Smuzhiyun   ``recipes-kernel`` directory holds your append file and eventual
394*4882a593Smuzhiyun   patch files.
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun2. *Create the Layer Configuration File*: Move to the
397*4882a593Smuzhiyun   ``meta-mylayer/conf`` directory and create the ``layer.conf`` file as
398*4882a593Smuzhiyun   follows::
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun      # We have a conf and classes directory, add to BBPATH
401*4882a593Smuzhiyun      BBPATH .= ":${LAYERDIR}"
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun      # We have recipes-* directories, add to BBFILES
404*4882a593Smuzhiyun      BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
405*4882a593Smuzhiyun                  ${LAYERDIR}/recipes-*/*/*.bbappend"
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun      BBFILE_COLLECTIONS += "mylayer"
408*4882a593Smuzhiyun      BBFILE_PATTERN_mylayer = "^${LAYERDIR}/"
409*4882a593Smuzhiyun      BBFILE_PRIORITY_mylayer = "5"
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun   Notice ``mylayer`` as part of the last three statements.
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun3. *Create the Kernel Recipe Append File*: Move to the
414*4882a593Smuzhiyun   ``meta-mylayer/recipes-kernel/linux`` directory and create the
415*4882a593Smuzhiyun   kernel's append file. This example uses the ``linux-yocto-4.12``
416*4882a593Smuzhiyun   kernel. Thus, the name of the append file is
417*4882a593Smuzhiyun   ``linux-yocto_4.12.bbappend``::
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun      FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun      SRC_URI:append = " file://patch-file-one.patch"
422*4882a593Smuzhiyun      SRC_URI:append = " file://patch-file-two.patch"
423*4882a593Smuzhiyun      SRC_URI:append = " file://patch-file-three.patch"
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun   The :term:`FILESEXTRAPATHS` and :term:`SRC_URI` statements
426*4882a593Smuzhiyun   enable the OpenEmbedded build system to find patch files. For more
427*4882a593Smuzhiyun   information on using append files, see the
428*4882a593Smuzhiyun   ":ref:`dev-manual/common-tasks:appending other layers metadata with your layer`"
429*4882a593Smuzhiyun   section in the Yocto Project Development Tasks Manual.
430*4882a593Smuzhiyun
431*4882a593SmuzhiyunModifying an Existing Recipe
432*4882a593Smuzhiyun============================
433*4882a593Smuzhiyun
434*4882a593SmuzhiyunIn many cases, you can customize an existing linux-yocto recipe to meet
435*4882a593Smuzhiyunthe needs of your project. Each release of the Yocto Project provides a
436*4882a593Smuzhiyunfew Linux kernel recipes from which you can choose. These are located in
437*4882a593Smuzhiyunthe :term:`Source Directory` in
438*4882a593Smuzhiyun``meta/recipes-kernel/linux``.
439*4882a593Smuzhiyun
440*4882a593SmuzhiyunModifying an existing recipe can consist of the following:
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun- :ref:`kernel-dev/common:creating the append file`
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun- :ref:`kernel-dev/common:applying patches`
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun- :ref:`kernel-dev/common:changing the configuration`
447*4882a593Smuzhiyun
448*4882a593SmuzhiyunBefore modifying an existing recipe, be sure that you have created a
449*4882a593Smuzhiyunminimal, custom layer from which you can work. See the
450*4882a593Smuzhiyun":ref:`kernel-dev/common:creating and preparing a layer`" section for
451*4882a593Smuzhiyuninformation.
452*4882a593Smuzhiyun
453*4882a593SmuzhiyunCreating the Append File
454*4882a593Smuzhiyun------------------------
455*4882a593Smuzhiyun
456*4882a593SmuzhiyunYou create this file in your custom layer. You also name it accordingly
457*4882a593Smuzhiyunbased on the linux-yocto recipe you are using. For example, if you are
458*4882a593Smuzhiyunmodifying the ``meta/recipes-kernel/linux/linux-yocto_4.12.bb`` recipe,
459*4882a593Smuzhiyunthe append file will typically be located as follows within your custom
460*4882a593Smuzhiyunlayer:
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun.. code-block:: none
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun   your-layer/recipes-kernel/linux/linux-yocto_4.12.bbappend
465*4882a593Smuzhiyun
466*4882a593SmuzhiyunThe append file should initially extend the
467*4882a593Smuzhiyun:term:`FILESPATH` search path by
468*4882a593Smuzhiyunprepending the directory that contains your files to the
469*4882a593Smuzhiyun:term:`FILESEXTRAPATHS`
470*4882a593Smuzhiyunvariable as follows::
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun   FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
473*4882a593Smuzhiyun
474*4882a593SmuzhiyunThe path ``${``\ :term:`THISDIR`\ ``}/${``\ :term:`PN`\ ``}``
475*4882a593Smuzhiyunexpands to "linux-yocto" in the current directory for this example. If
476*4882a593Smuzhiyunyou add any new files that modify the kernel recipe and you have
477*4882a593Smuzhiyunextended :term:`FILESPATH` as described above, you must place the files in
478*4882a593Smuzhiyunyour layer in the following area::
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun   your-layer/recipes-kernel/linux/linux-yocto/
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun.. note::
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun   If you are working on a new machine Board Support Package (BSP), be
485*4882a593Smuzhiyun   sure to refer to the :doc:`/bsp-guide/index`.
486*4882a593Smuzhiyun
487*4882a593SmuzhiyunAs an example, consider the following append file used by the BSPs in
488*4882a593Smuzhiyun``meta-yocto-bsp``:
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun.. code-block:: none
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun   meta-yocto-bsp/recipes-kernel/linux/linux-yocto_4.12.bbappend
493*4882a593Smuzhiyun
494*4882a593SmuzhiyunHere are the contents of this file. Be aware that the actual commit ID
495*4882a593Smuzhiyunstrings in this example listing might be different than the actual
496*4882a593Smuzhiyunstrings in the file from the ``meta-yocto-bsp`` layer upstream.
497*4882a593Smuzhiyun::
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun   KBRANCH:genericx86  = "standard/base"
500*4882a593Smuzhiyun   KBRANCH:genericx86-64  = "standard/base"
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun   KMACHINE:genericx86 ?= "common-pc"
503*4882a593Smuzhiyun   KMACHINE:genericx86-64 ?= "common-pc-64"
504*4882a593Smuzhiyun   KBRANCH:edgerouter = "standard/edgerouter"
505*4882a593Smuzhiyun   KBRANCH:beaglebone = "standard/beaglebone"
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun   SRCREV_machine:genericx86    ?= "d09f2ce584d60ecb7890550c22a80c48b83c2e19"
508*4882a593Smuzhiyun   SRCREV_machine:genericx86-64 ?= "d09f2ce584d60ecb7890550c22a80c48b83c2e19"
509*4882a593Smuzhiyun   SRCREV_machine:edgerouter ?= "b5c8cfda2dfe296410d51e131289fb09c69e1e7d"
510*4882a593Smuzhiyun   SRCREV_machine:beaglebone ?= "b5c8cfda2dfe296410d51e131289fb09c69e1e7d"
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun   COMPATIBLE_MACHINE:genericx86 = "genericx86"
514*4882a593Smuzhiyun   COMPATIBLE_MACHINE:genericx86-64 = "genericx86-64"
515*4882a593Smuzhiyun   COMPATIBLE_MACHINE:edgerouter = "edgerouter"
516*4882a593Smuzhiyun   COMPATIBLE_MACHINE:beaglebone = "beaglebone"
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun   LINUX_VERSION:genericx86 = "4.12.7"
519*4882a593Smuzhiyun   LINUX_VERSION:genericx86-64 = "4.12.7"
520*4882a593Smuzhiyun   LINUX_VERSION:edgerouter = "4.12.10"
521*4882a593Smuzhiyun   LINUX_VERSION:beaglebone = "4.12.10"
522*4882a593Smuzhiyun
523*4882a593SmuzhiyunThis append file
524*4882a593Smuzhiyuncontains statements used to support several BSPs that ship with the
525*4882a593SmuzhiyunYocto Project. The file defines machines using the
526*4882a593Smuzhiyun:term:`COMPATIBLE_MACHINE`
527*4882a593Smuzhiyunvariable and uses the
528*4882a593Smuzhiyun:term:`KMACHINE` variable to ensure
529*4882a593Smuzhiyunthe machine name used by the OpenEmbedded build system maps to the
530*4882a593Smuzhiyunmachine name used by the Linux Yocto kernel. The file also uses the
531*4882a593Smuzhiyunoptional :term:`KBRANCH` variable to
532*4882a593Smuzhiyunensure the build process uses the appropriate kernel branch.
533*4882a593Smuzhiyun
534*4882a593SmuzhiyunAlthough this particular example does not use it, the
535*4882a593Smuzhiyun:term:`KERNEL_FEATURES`
536*4882a593Smuzhiyunvariable could be used to enable features specific to the kernel. The
537*4882a593Smuzhiyunappend file points to specific commits in the
538*4882a593Smuzhiyun:term:`Source Directory` Git repository and
539*4882a593Smuzhiyunthe ``meta`` Git repository branches to identify the exact kernel needed
540*4882a593Smuzhiyunto build the BSP.
541*4882a593Smuzhiyun
542*4882a593SmuzhiyunOne thing missing in this particular BSP, which you will typically need
543*4882a593Smuzhiyunwhen developing a BSP, is the kernel configuration file (``.config``)
544*4882a593Smuzhiyunfor your BSP. When developing a BSP, you probably have a kernel
545*4882a593Smuzhiyunconfiguration file or a set of kernel configuration files that, when
546*4882a593Smuzhiyuntaken together, define the kernel configuration for your BSP. You can
547*4882a593Smuzhiyunaccomplish this definition by putting the configurations in a file or a
548*4882a593Smuzhiyunset of files inside a directory located at the same level as your
549*4882a593Smuzhiyunkernel's append file and having the same name as the kernel's main
550*4882a593Smuzhiyunrecipe file. With all these conditions met, simply reference those files
551*4882a593Smuzhiyunin the :term:`SRC_URI` statement in
552*4882a593Smuzhiyunthe append file.
553*4882a593Smuzhiyun
554*4882a593SmuzhiyunFor example, suppose you had some configuration options in a file called
555*4882a593Smuzhiyun``network_configs.cfg``. You can place that file inside a directory
556*4882a593Smuzhiyunnamed ``linux-yocto`` and then add a :term:`SRC_URI` statement such as the
557*4882a593Smuzhiyunfollowing to the append file. When the OpenEmbedded build system builds
558*4882a593Smuzhiyunthe kernel, the configuration options are picked up and applied.
559*4882a593Smuzhiyun::
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun   SRC_URI += "file://network_configs.cfg"
562*4882a593Smuzhiyun
563*4882a593SmuzhiyunTo group related configurations into multiple files, you perform a
564*4882a593Smuzhiyunsimilar procedure. Here is an example that groups separate
565*4882a593Smuzhiyunconfigurations specifically for Ethernet and graphics into their own
566*4882a593Smuzhiyunfiles and adds the configurations by using a :term:`SRC_URI` statement like
567*4882a593Smuzhiyunthe following in your append file::
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun   SRC_URI += "file://myconfig.cfg \
570*4882a593Smuzhiyun               file://eth.cfg \
571*4882a593Smuzhiyun               file://gfx.cfg"
572*4882a593Smuzhiyun
573*4882a593SmuzhiyunAnother variable you can use in your kernel recipe append file is the
574*4882a593Smuzhiyun:term:`FILESEXTRAPATHS`
575*4882a593Smuzhiyunvariable. When you use this statement, you are extending the locations
576*4882a593Smuzhiyunused by the OpenEmbedded system to look for files and patches as the
577*4882a593Smuzhiyunrecipe is processed.
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun.. note::
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun   There are other ways of grouping and defining configuration
582*4882a593Smuzhiyun   options. For example, if you are working with a local clone of the
583*4882a593Smuzhiyun   kernel repository, you could checkout the kernel's ``meta`` branch,
584*4882a593Smuzhiyun   make your changes, and then push the changes to the local bare clone
585*4882a593Smuzhiyun   of the kernel. The result is that you directly add configuration
586*4882a593Smuzhiyun   options to the ``meta`` branch for your BSP. The configuration
587*4882a593Smuzhiyun   options will likely end up in that location anyway if the BSP gets
588*4882a593Smuzhiyun   added to the Yocto Project.
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun   In general, however, the Yocto Project maintainers take care of
591*4882a593Smuzhiyun   moving the :term:`SRC_URI`-specified configuration options to the
592*4882a593Smuzhiyun   kernel's ``meta`` branch. Not only is it easier for BSP developers
593*4882a593Smuzhiyun   not to have to put those configurations in the branch,
594*4882a593Smuzhiyun   but having the maintainers do it allows them to apply 'global'
595*4882a593Smuzhiyun   knowledge about the kinds of common configuration options multiple
596*4882a593Smuzhiyun   BSPs in the tree are typically using. This allows for promotion of
597*4882a593Smuzhiyun   common configurations into common features.
598*4882a593Smuzhiyun
599*4882a593SmuzhiyunApplying Patches
600*4882a593Smuzhiyun----------------
601*4882a593Smuzhiyun
602*4882a593SmuzhiyunIf you have a single patch or a small series of patches that you want to
603*4882a593Smuzhiyunapply to the Linux kernel source, you can do so just as you would with
604*4882a593Smuzhiyunany other recipe. You first copy the patches to the path added to
605*4882a593Smuzhiyun:term:`FILESEXTRAPATHS` in
606*4882a593Smuzhiyunyour ``.bbappend`` file as described in the previous section, and then
607*4882a593Smuzhiyunreference them in :term:`SRC_URI`
608*4882a593Smuzhiyunstatements.
609*4882a593Smuzhiyun
610*4882a593SmuzhiyunFor example, you can apply a three-patch series by adding the following
611*4882a593Smuzhiyunlines to your linux-yocto ``.bbappend`` file in your layer::
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun   SRC_URI += "file://0001-first-change.patch"
614*4882a593Smuzhiyun   SRC_URI += "file://0002-second-change.patch"
615*4882a593Smuzhiyun   SRC_URI += "file://0003-third-change.patch"
616*4882a593Smuzhiyun
617*4882a593SmuzhiyunThe next time you run BitBake to build
618*4882a593Smuzhiyunthe Linux kernel, BitBake detects the change in the recipe and fetches
619*4882a593Smuzhiyunand applies the patches before building the kernel.
620*4882a593Smuzhiyun
621*4882a593SmuzhiyunFor a detailed example showing how to patch the kernel using
622*4882a593Smuzhiyun``devtool``, see the
623*4882a593Smuzhiyun":ref:`kernel-dev/common:using \`\`devtool\`\` to patch the kernel`"
624*4882a593Smuzhiyunand
625*4882a593Smuzhiyun":ref:`kernel-dev/common:using traditional kernel development to patch the kernel`"
626*4882a593Smuzhiyunsections.
627*4882a593Smuzhiyun
628*4882a593SmuzhiyunChanging the Configuration
629*4882a593Smuzhiyun--------------------------
630*4882a593Smuzhiyun
631*4882a593SmuzhiyunYou can make wholesale or incremental changes to the final ``.config``
632*4882a593Smuzhiyunfile used for the eventual Linux kernel configuration by including a
633*4882a593Smuzhiyun``defconfig`` file and by specifying configuration fragments in the
634*4882a593Smuzhiyun:term:`SRC_URI` to be applied to that
635*4882a593Smuzhiyunfile.
636*4882a593Smuzhiyun
637*4882a593SmuzhiyunIf you have a complete, working Linux kernel ``.config`` file you want
638*4882a593Smuzhiyunto use for the configuration, as before, copy that file to the
639*4882a593Smuzhiyunappropriate ``${PN}`` directory in your layer's ``recipes-kernel/linux``
640*4882a593Smuzhiyundirectory, and rename the copied file to "defconfig". Then, add the
641*4882a593Smuzhiyunfollowing lines to the linux-yocto ``.bbappend`` file in your layer::
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun   FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
644*4882a593Smuzhiyun   SRC_URI += "file://defconfig"
645*4882a593Smuzhiyun
646*4882a593SmuzhiyunThe :term:`SRC_URI` tells the build system how to search
647*4882a593Smuzhiyunfor the file, while the
648*4882a593Smuzhiyun:term:`FILESEXTRAPATHS`
649*4882a593Smuzhiyunextends the :term:`FILESPATH`
650*4882a593Smuzhiyunvariable (search directories) to include the ``${PN}`` directory you
651*4882a593Smuzhiyuncreated to hold the configuration changes.
652*4882a593Smuzhiyun
653*4882a593SmuzhiyunYou can also use a regular ``defconfig`` file, as generated by the
654*4882a593Smuzhiyun:ref:`ref-tasks-savedefconfig`
655*4882a593Smuzhiyuntask instead of a complete ``.config`` file. This only specifies the
656*4882a593Smuzhiyunnon-default configuration values.  You need to additionally set
657*4882a593Smuzhiyun:term:`KCONFIG_MODE`
658*4882a593Smuzhiyunin the linux-yocto ``.bbappend`` file in your layer::
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun   KCONFIG_MODE = "alldefconfig"
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun.. note::
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun   The build system applies the configurations from the ``defconfig``
665*4882a593Smuzhiyun   file before applying any subsequent configuration fragments. The
666*4882a593Smuzhiyun   final kernel configuration is a combination of the configurations in
667*4882a593Smuzhiyun   the ``defconfig`` file and any configuration fragments you provide. You need
668*4882a593Smuzhiyun   to realize that if you have any configuration fragments, the build system
669*4882a593Smuzhiyun   applies these on top of and after applying the existing ``defconfig`` file
670*4882a593Smuzhiyun   configurations.
671*4882a593Smuzhiyun
672*4882a593SmuzhiyunGenerally speaking, the preferred approach is to determine the
673*4882a593Smuzhiyunincremental change you want to make and add that as a configuration
674*4882a593Smuzhiyunfragment. For example, if you want to add support for a basic serial
675*4882a593Smuzhiyunconsole, create a file named ``8250.cfg`` in the ``${PN}`` directory
676*4882a593Smuzhiyunwith the following content (without indentation)::
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun   CONFIG_SERIAL_8250=y
679*4882a593Smuzhiyun   CONFIG_SERIAL_8250_CONSOLE=y
680*4882a593Smuzhiyun   CONFIG_SERIAL_8250_PCI=y
681*4882a593Smuzhiyun   CONFIG_SERIAL_8250_NR_UARTS=4
682*4882a593Smuzhiyun   CONFIG_SERIAL_8250_RUNTIME_UARTS=4
683*4882a593Smuzhiyun   CONFIG_SERIAL_CORE=y
684*4882a593Smuzhiyun   CONFIG_SERIAL_CORE_CONSOLE=y
685*4882a593Smuzhiyun
686*4882a593SmuzhiyunNext, include this
687*4882a593Smuzhiyunconfiguration fragment and extend the :term:`FILESPATH` variable in your
688*4882a593Smuzhiyun``.bbappend`` file::
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun   FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
691*4882a593Smuzhiyun   SRC_URI += "file://8250.cfg"
692*4882a593Smuzhiyun
693*4882a593SmuzhiyunThe next time you run BitBake to build the
694*4882a593SmuzhiyunLinux kernel, BitBake detects the change in the recipe and fetches and
695*4882a593Smuzhiyunapplies the new configuration before building the kernel.
696*4882a593Smuzhiyun
697*4882a593SmuzhiyunFor a detailed example showing how to configure the kernel, see the
698*4882a593Smuzhiyun":ref:`kernel-dev/common:configuring the kernel`" section.
699*4882a593Smuzhiyun
700*4882a593SmuzhiyunUsing an "In-Tree"  ``defconfig`` File
701*4882a593Smuzhiyun--------------------------------------
702*4882a593Smuzhiyun
703*4882a593SmuzhiyunIt might be desirable to have kernel configuration fragment support
704*4882a593Smuzhiyunthrough a ``defconfig`` file that is pulled from the kernel source tree
705*4882a593Smuzhiyunfor the configured machine. By default, the OpenEmbedded build system
706*4882a593Smuzhiyunlooks for ``defconfig`` files in the layer used for Metadata, which is
707*4882a593Smuzhiyun"out-of-tree", and then configures them using the following::
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun   SRC_URI += "file://defconfig"
710*4882a593Smuzhiyun
711*4882a593SmuzhiyunIf you do not want to maintain copies of
712*4882a593Smuzhiyun``defconfig`` files in your layer but would rather allow users to use
713*4882a593Smuzhiyunthe default configuration from the kernel tree and still be able to add
714*4882a593Smuzhiyunconfiguration fragments to the
715*4882a593Smuzhiyun:term:`SRC_URI` through, for example,
716*4882a593Smuzhiyunappend files, you can direct the OpenEmbedded build system to use a
717*4882a593Smuzhiyun``defconfig`` file that is "in-tree".
718*4882a593Smuzhiyun
719*4882a593SmuzhiyunTo specify an "in-tree" ``defconfig`` file, use the following statement
720*4882a593Smuzhiyunform::
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun   KBUILD_DEFCONFIG_KMACHINE ?= "defconfig_file"
723*4882a593Smuzhiyun
724*4882a593SmuzhiyunHere is an example
725*4882a593Smuzhiyunthat assigns the :term:`KBUILD_DEFCONFIG` variable based on "raspberrypi2"
726*4882a593Smuzhiyunand provides the path to the "in-tree" ``defconfig`` file to be used for
727*4882a593Smuzhiyuna Raspberry Pi 2, which is based on the Broadcom 2708/2709 chipset::
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun   KBUILD_DEFCONFIG:raspberrypi2 ?= "bcm2709_defconfig"
730*4882a593Smuzhiyun
731*4882a593SmuzhiyunAside from modifying your kernel recipe and providing your own
732*4882a593Smuzhiyun``defconfig`` file, you need to be sure no files or statements set
733*4882a593Smuzhiyun:term:`SRC_URI` to use a ``defconfig`` other than your "in-tree" file (e.g.
734*4882a593Smuzhiyuna kernel's ``linux-``\ `machine`\ ``.inc`` file). In other words, if the
735*4882a593Smuzhiyunbuild system detects a statement that identifies an "out-of-tree"
736*4882a593Smuzhiyun``defconfig`` file, that statement will override your
737*4882a593Smuzhiyun:term:`KBUILD_DEFCONFIG` variable.
738*4882a593Smuzhiyun
739*4882a593SmuzhiyunSee the
740*4882a593Smuzhiyun:term:`KBUILD_DEFCONFIG`
741*4882a593Smuzhiyunvariable description for more information.
742*4882a593Smuzhiyun
743*4882a593SmuzhiyunUsing ``devtool`` to Patch the Kernel
744*4882a593Smuzhiyun=====================================
745*4882a593Smuzhiyun
746*4882a593SmuzhiyunThe steps in this procedure show you how you can patch the kernel using
747*4882a593Smuzhiyunthe extensible SDK and ``devtool``.
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun.. note::
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun   Before attempting this procedure, be sure you have performed the
752*4882a593Smuzhiyun   steps to get ready for updating the kernel as described in the
753*4882a593Smuzhiyun   ":ref:`kernel-dev/common:getting ready to develop using \`\`devtool\`\``"
754*4882a593Smuzhiyun   section.
755*4882a593Smuzhiyun
756*4882a593SmuzhiyunPatching the kernel involves changing or adding configurations to an
757*4882a593Smuzhiyunexisting kernel, changing or adding recipes to the kernel that are
758*4882a593Smuzhiyunneeded to support specific hardware features, or even altering the
759*4882a593Smuzhiyunsource code itself.
760*4882a593Smuzhiyun
761*4882a593SmuzhiyunThis example creates a simple patch by adding some QEMU emulator console
762*4882a593Smuzhiyunoutput at boot time through ``printk`` statements in the kernel's
763*4882a593Smuzhiyun``calibrate.c`` source code file. Applying the patch and booting the
764*4882a593Smuzhiyunmodified image causes the added messages to appear on the emulator's
765*4882a593Smuzhiyunconsole. The example is a continuation of the setup procedure found in
766*4882a593Smuzhiyunthe ":ref:`kernel-dev/common:getting ready to develop using \`\`devtool\`\``" Section.
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun1. *Check Out the Kernel Source Files:* First you must use ``devtool``
769*4882a593Smuzhiyun   to checkout the kernel source code in its workspace. Be sure you are
770*4882a593Smuzhiyun   in the terminal set up to do work with the extensible SDK.
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun   .. note::
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun      See this step in the
775*4882a593Smuzhiyun      ":ref:`kernel-dev/common:getting ready to develop using \`\`devtool\`\``"
776*4882a593Smuzhiyun      section for more information.
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun   Use the following ``devtool`` command to check out the code::
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun      $ devtool modify linux-yocto
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun   .. note::
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun      During the checkout operation, there is a bug that could cause
785*4882a593Smuzhiyun      errors such as the following:
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun      .. code-block:: none
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun              ERROR: Taskhash mismatch 2c793438c2d9f8c3681fd5f7bc819efa versus
790*4882a593Smuzhiyun                     be3a89ce7c47178880ba7bf6293d7404 for
791*4882a593Smuzhiyun                     /path/to/esdk/layers/poky/meta/recipes-kernel/linux/linux-yocto_4.10.bb.do_unpack
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun      You can safely ignore these messages. The source code is correctly
795*4882a593Smuzhiyun      checked out.
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun2. *Edit the Source Files* Follow these steps to make some simple
798*4882a593Smuzhiyun   changes to the source files:
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun   1. *Change the working directory*: In the previous step, the output
801*4882a593Smuzhiyun      noted where you can find the source files (e.g.
802*4882a593Smuzhiyun      ``poky_sdk/workspace/sources/linux-yocto``). Change to where the
803*4882a593Smuzhiyun      kernel source code is before making your edits to the
804*4882a593Smuzhiyun      ``calibrate.c`` file::
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun         $ cd poky_sdk/workspace/sources/linux-yocto
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun   2. *Edit the source file*: Edit the ``init/calibrate.c`` file to have
809*4882a593Smuzhiyun      the following changes::
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun         void calibrate_delay(void)
812*4882a593Smuzhiyun         {
813*4882a593Smuzhiyun             unsigned long lpj;
814*4882a593Smuzhiyun             static bool printed;
815*4882a593Smuzhiyun             int this_cpu = smp_processor_id();
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun             printk("*************************************\n");
818*4882a593Smuzhiyun             printk("*                                   *\n");
819*4882a593Smuzhiyun             printk("*        HELLO YOCTO KERNEL         *\n");
820*4882a593Smuzhiyun             printk("*                                   *\n");
821*4882a593Smuzhiyun             printk("*************************************\n");
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun             if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
824*4882a593Smuzhiyun                   .
825*4882a593Smuzhiyun                   .
826*4882a593Smuzhiyun                   .
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun3. *Build the Updated Kernel Source:* To build the updated kernel
829*4882a593Smuzhiyun   source, use ``devtool``::
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun      $ devtool build linux-yocto
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun4. *Create the Image With the New Kernel:* Use the
834*4882a593Smuzhiyun   ``devtool build-image`` command to create a new image that has the
835*4882a593Smuzhiyun   new kernel.
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun   .. note::
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun      If the image you originally created resulted in a Wic file, you
840*4882a593Smuzhiyun      can use an alternate method to create the new image with the
841*4882a593Smuzhiyun      updated kernel. For an example, see the steps in the
842*4882a593Smuzhiyun      :yocto_wiki:`TipsAndTricks/KernelDevelopmentWithEsdk </TipsAndTricks/KernelDevelopmentWithEsdk>`
843*4882a593Smuzhiyun      Wiki Page.
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun   ::
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun      $ cd ~
848*4882a593Smuzhiyun      $ devtool build-image core-image-minimal
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun5. *Test the New Image:* For this example, you can run the new image
851*4882a593Smuzhiyun   using QEMU to verify your changes:
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun   1. *Boot the image*: Boot the modified image in the QEMU emulator
854*4882a593Smuzhiyun      using this command::
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun         $ runqemu qemux86
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun   2. *Verify the changes*: Log into the machine using ``root`` with no
859*4882a593Smuzhiyun      password and then use the following shell command to scroll
860*4882a593Smuzhiyun      through the console's boot output.
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun      .. code-block:: none
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun         # dmesg | less
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun      You should see
867*4882a593Smuzhiyun      the results of your ``printk`` statements as part of the output
868*4882a593Smuzhiyun      when you scroll down the console window.
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun6. *Stage and commit your changes*: Within your eSDK terminal, change
871*4882a593Smuzhiyun   your working directory to where you modified the ``calibrate.c`` file
872*4882a593Smuzhiyun   and use these Git commands to stage and commit your changes::
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun      $ cd poky_sdk/workspace/sources/linux-yocto
875*4882a593Smuzhiyun      $ git status
876*4882a593Smuzhiyun      $ git add init/calibrate.c
877*4882a593Smuzhiyun      $ git commit -m "calibrate: Add printk example"
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun7. *Export the Patches and Create an Append File:* To export your
880*4882a593Smuzhiyun   commits as patches and create a ``.bbappend`` file, use the following
881*4882a593Smuzhiyun   command in the terminal used to work with the extensible SDK. This
882*4882a593Smuzhiyun   example uses the previously established layer named ``meta-mylayer``.
883*4882a593Smuzhiyun   ::
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun      $ devtool finish linux-yocto ~/meta-mylayer
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun   .. note::
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun      See Step 3 of the
890*4882a593Smuzhiyun      ":ref:`kernel-dev/common:getting ready to develop using \`\`devtool\`\``"
891*4882a593Smuzhiyun      section for information on setting up this layer.
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun   Once the command
894*4882a593Smuzhiyun   finishes, the patches and the ``.bbappend`` file are located in the
895*4882a593Smuzhiyun   ``~/meta-mylayer/recipes-kernel/linux`` directory.
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun8. *Build the Image With Your Modified Kernel:* You can now build an
898*4882a593Smuzhiyun   image that includes your kernel patches. Execute the following
899*4882a593Smuzhiyun   command from your
900*4882a593Smuzhiyun   :term:`Build Directory` in the terminal
901*4882a593Smuzhiyun   set up to run BitBake::
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun      $ cd poky/build
904*4882a593Smuzhiyun      $ bitbake core-image-minimal
905*4882a593Smuzhiyun
906*4882a593SmuzhiyunUsing Traditional Kernel Development to Patch the Kernel
907*4882a593Smuzhiyun========================================================
908*4882a593Smuzhiyun
909*4882a593SmuzhiyunThe steps in this procedure show you how you can patch the kernel using
910*4882a593Smuzhiyuntraditional kernel development (i.e. not using ``devtool`` and the
911*4882a593Smuzhiyunextensible SDK as described in the
912*4882a593Smuzhiyun":ref:`kernel-dev/common:using \`\`devtool\`\` to patch the kernel`"
913*4882a593Smuzhiyunsection).
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun.. note::
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun   Before attempting this procedure, be sure you have performed the
918*4882a593Smuzhiyun   steps to get ready for updating the kernel as described in the
919*4882a593Smuzhiyun   ":ref:`kernel-dev/common:getting ready for traditional kernel development`"
920*4882a593Smuzhiyun   section.
921*4882a593Smuzhiyun
922*4882a593SmuzhiyunPatching the kernel involves changing or adding configurations to an
923*4882a593Smuzhiyunexisting kernel, changing or adding recipes to the kernel that are
924*4882a593Smuzhiyunneeded to support specific hardware features, or even altering the
925*4882a593Smuzhiyunsource code itself.
926*4882a593Smuzhiyun
927*4882a593SmuzhiyunThe example in this section creates a simple patch by adding some QEMU
928*4882a593Smuzhiyunemulator console output at boot time through ``printk`` statements in
929*4882a593Smuzhiyunthe kernel's ``calibrate.c`` source code file. Applying the patch and
930*4882a593Smuzhiyunbooting the modified image causes the added messages to appear on the
931*4882a593Smuzhiyunemulator's console. The example is a continuation of the setup procedure
932*4882a593Smuzhiyunfound in the
933*4882a593Smuzhiyun":ref:`kernel-dev/common:getting ready for traditional kernel development`"
934*4882a593SmuzhiyunSection.
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun1. *Edit the Source Files* Prior to this step, you should have used Git
937*4882a593Smuzhiyun   to create a local copy of the repository for your kernel. Assuming
938*4882a593Smuzhiyun   you created the repository as directed in the
939*4882a593Smuzhiyun   ":ref:`kernel-dev/common:getting ready for traditional kernel development`"
940*4882a593Smuzhiyun   section, use the following commands to edit the ``calibrate.c`` file:
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun   1. *Change the working directory*: You need to locate the source
943*4882a593Smuzhiyun      files in the local copy of the kernel Git repository. Change to
944*4882a593Smuzhiyun      where the kernel source code is before making your edits to the
945*4882a593Smuzhiyun      ``calibrate.c`` file::
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun         $ cd ~/linux-yocto-4.12/init
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun   2. *Edit the source file*: Edit the ``calibrate.c`` file to have the
950*4882a593Smuzhiyun      following changes::
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun         void calibrate_delay(void)
953*4882a593Smuzhiyun         {
954*4882a593Smuzhiyun             unsigned long lpj;
955*4882a593Smuzhiyun             static bool printed;
956*4882a593Smuzhiyun             int this_cpu = smp_processor_id();
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun             printk("*************************************\n");
959*4882a593Smuzhiyun             printk("*                                   *\n");
960*4882a593Smuzhiyun             printk("*        HELLO YOCTO KERNEL         *\n");
961*4882a593Smuzhiyun             printk("*                                   *\n");
962*4882a593Smuzhiyun             printk("*************************************\n");
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun             if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
965*4882a593Smuzhiyun                   .
966*4882a593Smuzhiyun                   .
967*4882a593Smuzhiyun                   .
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun2. *Stage and Commit Your Changes:* Use standard Git commands to stage
970*4882a593Smuzhiyun   and commit the changes you just made::
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun      $ git add calibrate.c
973*4882a593Smuzhiyun      $ git commit -m "calibrate.c - Added some printk statements"
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun   If you do not
976*4882a593Smuzhiyun   stage and commit your changes, the OpenEmbedded Build System will not
977*4882a593Smuzhiyun   pick up the changes.
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun3. *Update Your local.conf File to Point to Your Source Files:* In
980*4882a593Smuzhiyun   addition to your ``local.conf`` file specifying to use
981*4882a593Smuzhiyun   "kernel-modules" and the "qemux86" machine, it must also point to the
982*4882a593Smuzhiyun   updated kernel source files. Add
983*4882a593Smuzhiyun   :term:`SRC_URI` and
984*4882a593Smuzhiyun   :term:`SRCREV` statements similar
985*4882a593Smuzhiyun   to the following to your ``local.conf``::
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun      $ cd poky/build/conf
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun   Add the following to the ``local.conf``::
990*4882a593Smuzhiyun
991*4882a593Smuzhiyun      SRC_URI:pn-linux-yocto = "git:///path-to/linux-yocto-4.12;protocol=file;name=machine;branch=standard/base; \
992*4882a593Smuzhiyun                                git:///path-to/yocto-kernel-cache;protocol=file;type=kmeta;name=meta;branch=yocto-4.12;destsuffix=${KMETA}"
993*4882a593Smuzhiyun      SRCREV_meta:qemux86 = "${AUTOREV}"
994*4882a593Smuzhiyun      SRCREV_machine:qemux86 = "${AUTOREV}"
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun   .. note::
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun      Be sure to replace `path-to`
999*4882a593Smuzhiyun      with the pathname to your local Git repositories. Also, you must
1000*4882a593Smuzhiyun      be sure to specify the correct branch and machine types. For this
1001*4882a593Smuzhiyun      example, the branch is ``standard/base`` and the machine is ``qemux86``.
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun4. *Build the Image:* With the source modified, your changes staged and
1004*4882a593Smuzhiyun   committed, and the ``local.conf`` file pointing to the kernel files,
1005*4882a593Smuzhiyun   you can now use BitBake to build the image::
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun      $ cd poky/build
1008*4882a593Smuzhiyun      $ bitbake core-image-minimal
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun5. *Boot the image*: Boot the modified image in the QEMU emulator using
1011*4882a593Smuzhiyun   this command. When prompted to login to the QEMU console, use "root"
1012*4882a593Smuzhiyun   with no password::
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun      $ cd poky/build
1015*4882a593Smuzhiyun      $ runqemu qemux86
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun6. *Look for Your Changes:* As QEMU booted, you might have seen your
1018*4882a593Smuzhiyun   changes rapidly scroll by. If not, use these commands to see your
1019*4882a593Smuzhiyun   changes:
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun   .. code-block:: none
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun      # dmesg | less
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun   You should see the results of your
1026*4882a593Smuzhiyun   ``printk`` statements as part of the output when you scroll down the
1027*4882a593Smuzhiyun   console window.
1028*4882a593Smuzhiyun
1029*4882a593Smuzhiyun7. *Generate the Patch File:* Once you are sure that your patch works
1030*4882a593Smuzhiyun   correctly, you can generate a ``*.patch`` file in the kernel source
1031*4882a593Smuzhiyun   repository::
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun      $ cd ~/linux-yocto-4.12/init
1034*4882a593Smuzhiyun      $ git format-patch -1
1035*4882a593Smuzhiyun      0001-calibrate.c-Added-some-printk-statements.patch
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun8. *Move the Patch File to Your Layer:* In order for subsequent builds
1038*4882a593Smuzhiyun   to pick up patches, you need to move the patch file you created in
1039*4882a593Smuzhiyun   the previous step to your layer ``meta-mylayer``. For this example,
1040*4882a593Smuzhiyun   the layer created earlier is located in your home directory as
1041*4882a593Smuzhiyun   ``meta-mylayer``. When the layer was created using the
1042*4882a593Smuzhiyun   ``yocto-create`` script, no additional hierarchy was created to
1043*4882a593Smuzhiyun   support patches. Before moving the patch file, you need to add
1044*4882a593Smuzhiyun   additional structure to your layer using the following commands::
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun      $ cd ~/meta-mylayer
1047*4882a593Smuzhiyun      $ mkdir recipes-kernel
1048*4882a593Smuzhiyun      $ mkdir recipes-kernel/linux
1049*4882a593Smuzhiyun      $ mkdir recipes-kernel/linux/linux-yocto
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun   Once you have created this
1052*4882a593Smuzhiyun   hierarchy in your layer, you can move the patch file using the
1053*4882a593Smuzhiyun   following command::
1054*4882a593Smuzhiyun
1055*4882a593Smuzhiyun      $ mv ~/linux-yocto-4.12/init/0001-calibrate.c-Added-some-printk-statements.patch ~/meta-mylayer/recipes-kernel/linux/linux-yocto
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun9. *Create the Append File:* Finally, you need to create the
1058*4882a593Smuzhiyun   ``linux-yocto_4.12.bbappend`` file and insert statements that allow
1059*4882a593Smuzhiyun   the OpenEmbedded build system to find the patch. The append file
1060*4882a593Smuzhiyun   needs to be in your layer's ``recipes-kernel/linux`` directory and it
1061*4882a593Smuzhiyun   must be named ``linux-yocto_4.12.bbappend`` and have the following
1062*4882a593Smuzhiyun   contents::
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun      FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
1065*4882a593Smuzhiyun      SRC_URI:append = "file://0001-calibrate.c-Added-some-printk-statements.patch"
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun   The :term:`FILESEXTRAPATHS` and :term:`SRC_URI` statements
1068*4882a593Smuzhiyun   enable the OpenEmbedded build system to find the patch file.
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun   For more information on append files and patches, see the
1071*4882a593Smuzhiyun   ":ref:`kernel-dev/common:creating the append file`" and
1072*4882a593Smuzhiyun   ":ref:`kernel-dev/common:applying patches`" sections. You can also see the
1073*4882a593Smuzhiyun   ":ref:`dev-manual/common-tasks:appending other layers metadata with your layer`"
1074*4882a593Smuzhiyun   section in the Yocto Project Development Tasks Manual.
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun   .. note::
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun      To build ``core-image-minimal`` again and see the effects of your patch,
1079*4882a593Smuzhiyun      you can essentially eliminate the temporary source files saved in
1080*4882a593Smuzhiyun      ``poky/build/tmp/work/...`` and residual effects of the build by entering
1081*4882a593Smuzhiyun      the following sequence of commands::
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun              $ cd poky/build
1084*4882a593Smuzhiyun              $ bitbake -c cleanall yocto-linux
1085*4882a593Smuzhiyun              $ bitbake core-image-minimal -c cleanall
1086*4882a593Smuzhiyun              $ bitbake core-image-minimal
1087*4882a593Smuzhiyun              $ runqemu qemux86
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun
1090*4882a593SmuzhiyunConfiguring the Kernel
1091*4882a593Smuzhiyun======================
1092*4882a593Smuzhiyun
1093*4882a593SmuzhiyunConfiguring the Yocto Project kernel consists of making sure the
1094*4882a593Smuzhiyun``.config`` file has all the right information in it for the image you
1095*4882a593Smuzhiyunare building. You can use the ``menuconfig`` tool and configuration
1096*4882a593Smuzhiyunfragments to make sure your ``.config`` file is just how you need it.
1097*4882a593SmuzhiyunYou can also save known configurations in a ``defconfig`` file that the
1098*4882a593Smuzhiyunbuild system can use for kernel configuration.
1099*4882a593Smuzhiyun
1100*4882a593SmuzhiyunThis section describes how to use ``menuconfig``, create and use
1101*4882a593Smuzhiyunconfiguration fragments, and how to interactively modify your
1102*4882a593Smuzhiyun``.config`` file to create the leanest kernel configuration file
1103*4882a593Smuzhiyunpossible.
1104*4882a593Smuzhiyun
1105*4882a593SmuzhiyunFor more information on kernel configuration, see the
1106*4882a593Smuzhiyun":ref:`kernel-dev/common:changing the configuration`" section.
1107*4882a593Smuzhiyun
1108*4882a593SmuzhiyunUsing  ``menuconfig``
1109*4882a593Smuzhiyun---------------------
1110*4882a593Smuzhiyun
1111*4882a593SmuzhiyunThe easiest way to define kernel configurations is to set them through
1112*4882a593Smuzhiyunthe ``menuconfig`` tool. This tool provides an interactive method with
1113*4882a593Smuzhiyunwhich to set kernel configurations. For general information on
1114*4882a593Smuzhiyun``menuconfig``, see https://en.wikipedia.org/wiki/Menuconfig.
1115*4882a593Smuzhiyun
1116*4882a593SmuzhiyunTo use the ``menuconfig`` tool in the Yocto Project development
1117*4882a593Smuzhiyunenvironment, you must do the following:
1118*4882a593Smuzhiyun
1119*4882a593Smuzhiyun-  Because you launch ``menuconfig`` using BitBake, you must be sure to
1120*4882a593Smuzhiyun   set up your environment by running the
1121*4882a593Smuzhiyun   :ref:`structure-core-script` script found in
1122*4882a593Smuzhiyun   the :term:`Build Directory`.
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun-  You must be sure of the state of your build's configuration in the
1125*4882a593Smuzhiyun   :term:`Source Directory`.
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun-  Your build host must have the following two packages installed::
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun      libncurses5-dev
1130*4882a593Smuzhiyun      libtinfo-dev
1131*4882a593Smuzhiyun
1132*4882a593SmuzhiyunThe following commands initialize the BitBake environment, run the
1133*4882a593Smuzhiyun:ref:`ref-tasks-kernel_configme`
1134*4882a593Smuzhiyuntask, and launch ``menuconfig``. These commands assume the Source
1135*4882a593SmuzhiyunDirectory's top-level folder is ``poky``::
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun   $ cd poky
1138*4882a593Smuzhiyun   $ source oe-init-build-env
1139*4882a593Smuzhiyun   $ bitbake linux-yocto -c kernel_configme -f
1140*4882a593Smuzhiyun   $ bitbake linux-yocto -c menuconfig
1141*4882a593Smuzhiyun
1142*4882a593SmuzhiyunOnce ``menuconfig`` comes up, its standard
1143*4882a593Smuzhiyuninterface allows you to interactively examine and configure all the
1144*4882a593Smuzhiyunkernel configuration parameters. After making your changes, simply exit
1145*4882a593Smuzhiyunthe tool and save your changes to create an updated version of the
1146*4882a593Smuzhiyun``.config`` configuration file.
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun.. note::
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun   You can use the entire ``.config`` file as the ``defconfig`` file. For
1151*4882a593Smuzhiyun   information on ``defconfig`` files, see the
1152*4882a593Smuzhiyun   ":ref:`kernel-dev/common:changing the configuration`",
1153*4882a593Smuzhiyun   ":ref:`kernel-dev/common:using an "in-tree" \`\`defconfig\`\` file`",
1154*4882a593Smuzhiyun   and ":ref:`kernel-dev/common:creating a \`\`defconfig\`\` file`"
1155*4882a593Smuzhiyun   sections.
1156*4882a593Smuzhiyun
1157*4882a593SmuzhiyunConsider an example that configures the "CONFIG_SMP" setting for the
1158*4882a593Smuzhiyun``linux-yocto-4.12`` kernel.
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun.. note::
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun   The OpenEmbedded build system recognizes this kernel as ``linux-yocto``
1163*4882a593Smuzhiyun   through Metadata (e.g. :term:`PREFERRED_VERSION`\ ``_linux-yocto ?= "12.4%"``).
1164*4882a593Smuzhiyun
1165*4882a593SmuzhiyunOnce ``menuconfig`` launches, use the interface to navigate through the
1166*4882a593Smuzhiyunselections to find the configuration settings in which you are
1167*4882a593Smuzhiyuninterested. For this example, you deselect "CONFIG_SMP" by clearing the
1168*4882a593Smuzhiyun"Symmetric Multi-Processing Support" option. Using the interface, you
1169*4882a593Smuzhiyuncan find the option under "Processor Type and Features". To deselect
1170*4882a593Smuzhiyun"CONFIG_SMP", use the arrow keys to highlight "Symmetric
1171*4882a593SmuzhiyunMulti-Processing Support" and enter "N" to clear the asterisk. When you
1172*4882a593Smuzhiyunare finished, exit out and save the change.
1173*4882a593Smuzhiyun
1174*4882a593SmuzhiyunSaving the selections updates the ``.config`` configuration file. This
1175*4882a593Smuzhiyunis the file that the OpenEmbedded build system uses to configure the
1176*4882a593Smuzhiyunkernel during the build. You can find and examine this file in the Build
1177*4882a593SmuzhiyunDirectory in ``tmp/work/``. The actual ``.config`` is located in the
1178*4882a593Smuzhiyunarea where the specific kernel is built. For example, if you were
1179*4882a593Smuzhiyunbuilding a Linux Yocto kernel based on the ``linux-yocto-4.12`` kernel
1180*4882a593Smuzhiyunand you were building a QEMU image targeted for ``x86`` architecture,
1181*4882a593Smuzhiyunthe ``.config`` file would be:
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun.. code-block:: none
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun   poky/build/tmp/work/qemux86-poky-linux/linux-yocto/4.12.12+gitAUTOINC+eda4d18...
1186*4882a593Smuzhiyun   ...967-r0/linux-qemux86-standard-build/.config
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun.. note::
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun   The previous example directory is artificially split and many of the
1191*4882a593Smuzhiyun   characters in the actual filename are omitted in order to make it
1192*4882a593Smuzhiyun   more readable. Also, depending on the kernel you are using, the exact
1193*4882a593Smuzhiyun   pathname might differ.
1194*4882a593Smuzhiyun
1195*4882a593SmuzhiyunWithin the ``.config`` file, you can see the kernel settings. For
1196*4882a593Smuzhiyunexample, the following entry shows that symmetric multi-processor
1197*4882a593Smuzhiyunsupport is not set::
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun   # CONFIG_SMP is not set
1200*4882a593Smuzhiyun
1201*4882a593SmuzhiyunA good method to isolate changed configurations is to use a combination
1202*4882a593Smuzhiyunof the ``menuconfig`` tool and simple shell commands. Before changing
1203*4882a593Smuzhiyunconfigurations with ``menuconfig``, copy the existing ``.config`` and
1204*4882a593Smuzhiyunrename it to something else, use ``menuconfig`` to make as many changes
1205*4882a593Smuzhiyunas you want and save them, then compare the renamed configuration file
1206*4882a593Smuzhiyunagainst the newly created file. You can use the resulting differences as
1207*4882a593Smuzhiyunyour base to create configuration fragments to permanently save in your
1208*4882a593Smuzhiyunkernel layer.
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun.. note::
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun   Be sure to make a copy of the ``.config`` file and do not just rename it.
1213*4882a593Smuzhiyun   The build system needs an existing ``.config`` file from which to work.
1214*4882a593Smuzhiyun
1215*4882a593SmuzhiyunCreating a  ``defconfig`` File
1216*4882a593Smuzhiyun------------------------------
1217*4882a593Smuzhiyun
1218*4882a593SmuzhiyunA ``defconfig`` file in the context of the Yocto Project is often a
1219*4882a593Smuzhiyun``.config`` file that is copied from a build or a ``defconfig`` taken
1220*4882a593Smuzhiyunfrom the kernel tree and moved into recipe space. You can use a
1221*4882a593Smuzhiyun``defconfig`` file to retain a known set of kernel configurations from
1222*4882a593Smuzhiyunwhich the OpenEmbedded build system can draw to create the final
1223*4882a593Smuzhiyun``.config`` file.
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun.. note::
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun   Out-of-the-box, the Yocto Project never ships a ``defconfig`` or ``.config``
1228*4882a593Smuzhiyun   file. The OpenEmbedded build system creates the final ``.config`` file used
1229*4882a593Smuzhiyun   to configure the kernel.
1230*4882a593Smuzhiyun
1231*4882a593SmuzhiyunTo create a ``defconfig``, start with a complete, working Linux kernel
1232*4882a593Smuzhiyun``.config`` file. Copy that file to the appropriate
1233*4882a593Smuzhiyun``${``\ :term:`PN`\ ``}`` directory in
1234*4882a593Smuzhiyunyour layer's ``recipes-kernel/linux`` directory, and rename the copied
1235*4882a593Smuzhiyunfile to "defconfig" (e.g.
1236*4882a593Smuzhiyun``~/meta-mylayer/recipes-kernel/linux/linux-yocto/defconfig``). Then,
1237*4882a593Smuzhiyunadd the following lines to the linux-yocto ``.bbappend`` file in your
1238*4882a593Smuzhiyunlayer::
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun   FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
1241*4882a593Smuzhiyun   SRC_URI += "file://defconfig"
1242*4882a593Smuzhiyun
1243*4882a593SmuzhiyunThe :term:`SRC_URI` tells the build system how to search for the file, while the
1244*4882a593Smuzhiyun:term:`FILESEXTRAPATHS` extends the :term:`FILESPATH`
1245*4882a593Smuzhiyunvariable (search directories) to include the ``${PN}`` directory you
1246*4882a593Smuzhiyuncreated to hold the configuration changes.
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun.. note::
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun   The build system applies the configurations from the ``defconfig``
1251*4882a593Smuzhiyun   file before applying any subsequent configuration fragments. The
1252*4882a593Smuzhiyun   final kernel configuration is a combination of the configurations in
1253*4882a593Smuzhiyun   the ``defconfig`` file and any configuration fragments you provide. You need
1254*4882a593Smuzhiyun   to realize that if you have any configuration fragments, the build system
1255*4882a593Smuzhiyun   applies these on top of and after applying the existing ``defconfig`` file
1256*4882a593Smuzhiyun   configurations.
1257*4882a593Smuzhiyun
1258*4882a593SmuzhiyunFor more information on configuring the kernel, see the
1259*4882a593Smuzhiyun":ref:`kernel-dev/common:changing the configuration`" section.
1260*4882a593Smuzhiyun
1261*4882a593SmuzhiyunCreating Configuration Fragments
1262*4882a593Smuzhiyun--------------------------------
1263*4882a593Smuzhiyun
1264*4882a593SmuzhiyunConfiguration fragments are simply kernel options that appear in a file
1265*4882a593Smuzhiyunplaced where the OpenEmbedded build system can find and apply them. The
1266*4882a593Smuzhiyunbuild system applies configuration fragments after applying
1267*4882a593Smuzhiyunconfigurations from a ``defconfig`` file. Thus, the final kernel
1268*4882a593Smuzhiyunconfiguration is a combination of the configurations in the
1269*4882a593Smuzhiyun``defconfig`` file and then any configuration fragments you provide. The
1270*4882a593Smuzhiyunbuild system applies fragments on top of and after applying the existing
1271*4882a593Smuzhiyundefconfig file configurations.
1272*4882a593Smuzhiyun
1273*4882a593SmuzhiyunSyntactically, the configuration statement is identical to what would
1274*4882a593Smuzhiyunappear in the ``.config`` file, which is in the :term:`Build Directory`.
1275*4882a593Smuzhiyun
1276*4882a593Smuzhiyun.. note::
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun   For more information about where the ``.config`` file is located, see the
1279*4882a593Smuzhiyun   example in the
1280*4882a593Smuzhiyun   ":ref:`kernel-dev/common:using \`\`menuconfig\`\``"
1281*4882a593Smuzhiyun   section.
1282*4882a593Smuzhiyun
1283*4882a593SmuzhiyunIt is simple to create a configuration fragment. One method is to use
1284*4882a593Smuzhiyunshell commands. For example, issuing the following from the shell
1285*4882a593Smuzhiyuncreates a configuration fragment file named ``my_smp.cfg`` that enables
1286*4882a593Smuzhiyunmulti-processor support within the kernel::
1287*4882a593Smuzhiyun
1288*4882a593Smuzhiyun   $ echo "CONFIG_SMP=y" >> my_smp.cfg
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun.. note::
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun   All configuration fragment files must use the ``.cfg`` extension in order
1293*4882a593Smuzhiyun   for the OpenEmbedded build system to recognize them as a configuration
1294*4882a593Smuzhiyun   fragment.
1295*4882a593Smuzhiyun
1296*4882a593SmuzhiyunAnother method is to create a configuration fragment using the
1297*4882a593Smuzhiyundifferences between two configuration files: one previously created and
1298*4882a593Smuzhiyunsaved, and one freshly created using the ``menuconfig`` tool.
1299*4882a593Smuzhiyun
1300*4882a593SmuzhiyunTo create a configuration fragment using this method, follow these
1301*4882a593Smuzhiyunsteps:
1302*4882a593Smuzhiyun
1303*4882a593Smuzhiyun1. *Complete a Build Through Kernel Configuration:* Complete a build at
1304*4882a593Smuzhiyun   least through the kernel configuration task as follows::
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun      $ bitbake linux-yocto -c kernel_configme -f
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun   This step ensures that you create a
1309*4882a593Smuzhiyun   ``.config`` file from a known state. Because there are situations where
1310*4882a593Smuzhiyun   your build state might become unknown, it is best to run this task
1311*4882a593Smuzhiyun   prior to starting ``menuconfig``.
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun2. *Launch menuconfig:* Run the ``menuconfig`` command::
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun      $ bitbake linux-yocto -c menuconfig
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun3. *Create the Configuration Fragment:* Run the ``diffconfig`` command
1318*4882a593Smuzhiyun   to prepare a configuration fragment. The resulting file
1319*4882a593Smuzhiyun   ``fragment.cfg`` is placed in the
1320*4882a593Smuzhiyun   ``${``\ :term:`WORKDIR`\ ``}``
1321*4882a593Smuzhiyun   directory::
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun      $ bitbake linux-yocto -c diffconfig
1324*4882a593Smuzhiyun
1325*4882a593SmuzhiyunThe ``diffconfig`` command creates a file that is a list of Linux kernel
1326*4882a593Smuzhiyun``CONFIG_`` assignments. See the
1327*4882a593Smuzhiyun":ref:`kernel-dev/common:changing the configuration`" section for additional
1328*4882a593Smuzhiyuninformation on how to use the output as a configuration fragment.
1329*4882a593Smuzhiyun
1330*4882a593Smuzhiyun.. note::
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun   You can also use this method to create configuration fragments for a
1333*4882a593Smuzhiyun   BSP. See the ":ref:`kernel-dev/advanced:bsp descriptions`"
1334*4882a593Smuzhiyun   section for more information.
1335*4882a593Smuzhiyun
1336*4882a593SmuzhiyunWhere do you put your configuration fragment files? You can place these
1337*4882a593Smuzhiyunfiles in an area pointed to by
1338*4882a593Smuzhiyun:term:`SRC_URI` as directed by your
1339*4882a593Smuzhiyun``bblayers.conf`` file, which is located in your layer. The OpenEmbedded
1340*4882a593Smuzhiyunbuild system picks up the configuration and adds it to the kernel's
1341*4882a593Smuzhiyunconfiguration. For example, suppose you had a set of configuration
1342*4882a593Smuzhiyunoptions in a file called ``myconfig.cfg``. If you put that file inside a
1343*4882a593Smuzhiyundirectory named ``linux-yocto`` that resides in the same directory as
1344*4882a593Smuzhiyunthe kernel's append file within your layer and then add the following
1345*4882a593Smuzhiyunstatements to the kernel's append file, those configuration options will
1346*4882a593Smuzhiyunbe picked up and applied when the kernel is built::
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyun   FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
1349*4882a593Smuzhiyun   SRC_URI += "file://myconfig.cfg"
1350*4882a593Smuzhiyun
1351*4882a593SmuzhiyunAs mentioned earlier, you can group related configurations into multiple
1352*4882a593Smuzhiyunfiles and name them all in the :term:`SRC_URI` statement as well. For
1353*4882a593Smuzhiyunexample, you could group separate configurations specifically for
1354*4882a593SmuzhiyunEthernet and graphics into their own files and add those by using a
1355*4882a593Smuzhiyun:term:`SRC_URI` statement like the following in your append file::
1356*4882a593Smuzhiyun
1357*4882a593Smuzhiyun   SRC_URI += "file://myconfig.cfg \
1358*4882a593Smuzhiyun               file://eth.cfg \
1359*4882a593Smuzhiyun               file://gfx.cfg"
1360*4882a593Smuzhiyun
1361*4882a593SmuzhiyunValidating Configuration
1362*4882a593Smuzhiyun------------------------
1363*4882a593Smuzhiyun
1364*4882a593SmuzhiyunYou can use the
1365*4882a593Smuzhiyun:ref:`ref-tasks-kernel_configcheck`
1366*4882a593Smuzhiyuntask to provide configuration validation::
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun   $ bitbake linux-yocto -c kernel_configcheck -f
1369*4882a593Smuzhiyun
1370*4882a593SmuzhiyunRunning this task produces warnings for when a
1371*4882a593Smuzhiyunrequested configuration does not appear in the final ``.config`` file or
1372*4882a593Smuzhiyunwhen you override a policy configuration in a hardware configuration
1373*4882a593Smuzhiyunfragment.
1374*4882a593Smuzhiyun
1375*4882a593SmuzhiyunIn order to run this task, you must have an existing ``.config`` file.
1376*4882a593SmuzhiyunSee the ":ref:`kernel-dev/common:using \`\`menuconfig\`\``" section for
1377*4882a593Smuzhiyuninformation on how to create a configuration file.
1378*4882a593Smuzhiyun
1379*4882a593SmuzhiyunFollowing is sample output from the ``do_kernel_configcheck`` task:
1380*4882a593Smuzhiyun
1381*4882a593Smuzhiyun.. code-block:: none
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun   Loading cache: 100% |########################################################| Time: 0:00:00
1384*4882a593Smuzhiyun   Loaded 1275 entries from dependency cache.
1385*4882a593Smuzhiyun   NOTE: Resolving any missing task queue dependencies
1386*4882a593Smuzhiyun
1387*4882a593Smuzhiyun   Build Configuration:
1388*4882a593Smuzhiyun       .
1389*4882a593Smuzhiyun       .
1390*4882a593Smuzhiyun       .
1391*4882a593Smuzhiyun
1392*4882a593Smuzhiyun   NOTE: Executing SetScene Tasks
1393*4882a593Smuzhiyun   NOTE: Executing RunQueue Tasks
1394*4882a593Smuzhiyun   WARNING: linux-yocto-4.12.12+gitAUTOINC+eda4d18ce4_16de014967-r0 do_kernel_configcheck:
1395*4882a593Smuzhiyun       [kernel config]: specified values did not make it into the kernel's final configuration:
1396*4882a593Smuzhiyun
1397*4882a593Smuzhiyun   ---------- CONFIG_X86_TSC -----------------
1398*4882a593Smuzhiyun   Config: CONFIG_X86_TSC
1399*4882a593Smuzhiyun   From: /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/bsp/common-pc/common-pc-cpu.cfg
1400*4882a593Smuzhiyun   Requested value:  CONFIG_X86_TSC=y
1401*4882a593Smuzhiyun   Actual value:
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun   ---------- CONFIG_X86_BIGSMP -----------------
1405*4882a593Smuzhiyun   Config: CONFIG_X86_BIGSMP
1406*4882a593Smuzhiyun   From: /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/cfg/smp.cfg
1407*4882a593Smuzhiyun         /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/defconfig
1408*4882a593Smuzhiyun   Requested value:  # CONFIG_X86_BIGSMP is not set
1409*4882a593Smuzhiyun   Actual value:
1410*4882a593Smuzhiyun
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun   ---------- CONFIG_NR_CPUS -----------------
1413*4882a593Smuzhiyun   Config: CONFIG_NR_CPUS
1414*4882a593Smuzhiyun   From: /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/cfg/smp.cfg
1415*4882a593Smuzhiyun         /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/bsp/common-pc/common-pc.cfg
1416*4882a593Smuzhiyun         /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/defconfig
1417*4882a593Smuzhiyun   Requested value:  CONFIG_NR_CPUS=8
1418*4882a593Smuzhiyun   Actual value:     CONFIG_NR_CPUS=1
1419*4882a593Smuzhiyun
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun   ---------- CONFIG_SCHED_SMT -----------------
1422*4882a593Smuzhiyun   Config: CONFIG_SCHED_SMT
1423*4882a593Smuzhiyun   From: /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/cfg/smp.cfg
1424*4882a593Smuzhiyun         /home/scottrif/poky/build/tmp/work-shared/qemux86/kernel-source/.kernel-meta/configs/standard/defconfig
1425*4882a593Smuzhiyun   Requested value:  CONFIG_SCHED_SMT=y
1426*4882a593Smuzhiyun   Actual value:
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun   NOTE: Tasks Summary: Attempted 288 tasks of which 285 didn't need to be rerun and all succeeded.
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun   Summary: There were 3 WARNING messages shown.
1433*4882a593Smuzhiyun
1434*4882a593Smuzhiyun.. note::
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun   The previous output example has artificial line breaks to make it
1437*4882a593Smuzhiyun   more readable.
1438*4882a593Smuzhiyun
1439*4882a593SmuzhiyunThe output describes the various problems that you can encounter along
1440*4882a593Smuzhiyunwith where to find the offending configuration items. You can use the
1441*4882a593Smuzhiyuninformation in the logs to adjust your configuration files and then
1442*4882a593Smuzhiyunrepeat the
1443*4882a593Smuzhiyun:ref:`ref-tasks-kernel_configme`
1444*4882a593Smuzhiyunand
1445*4882a593Smuzhiyun:ref:`ref-tasks-kernel_configcheck`
1446*4882a593Smuzhiyuntasks until they produce no warnings.
1447*4882a593Smuzhiyun
1448*4882a593SmuzhiyunFor more information on how to use the ``menuconfig`` tool, see the
1449*4882a593Smuzhiyun:ref:`kernel-dev/common:using \`\`menuconfig\`\`` section.
1450*4882a593Smuzhiyun
1451*4882a593SmuzhiyunFine-Tuning the Kernel Configuration File
1452*4882a593Smuzhiyun-----------------------------------------
1453*4882a593Smuzhiyun
1454*4882a593SmuzhiyunYou can make sure the ``.config`` file is as lean or efficient as
1455*4882a593Smuzhiyunpossible by reading the output of the kernel configuration fragment
1456*4882a593Smuzhiyunaudit, noting any issues, making changes to correct the issues, and then
1457*4882a593Smuzhiyunrepeating.
1458*4882a593Smuzhiyun
1459*4882a593SmuzhiyunAs part of the kernel build process, the ``do_kernel_configcheck`` task
1460*4882a593Smuzhiyunruns. This task validates the kernel configuration by checking the final
1461*4882a593Smuzhiyun``.config`` file against the input files. During the check, the task
1462*4882a593Smuzhiyunproduces warning messages for the following issues:
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun-  Requested options that did not make the final ``.config`` file.
1465*4882a593Smuzhiyun
1466*4882a593Smuzhiyun-  Configuration items that appear twice in the same configuration
1467*4882a593Smuzhiyun   fragment.
1468*4882a593Smuzhiyun
1469*4882a593Smuzhiyun-  Configuration items tagged as "required" that were overridden.
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun-  A board overrides a non-board specific option.
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun-  Listed options not valid for the kernel being processed. In other
1474*4882a593Smuzhiyun   words, the option does not appear anywhere.
1475*4882a593Smuzhiyun
1476*4882a593Smuzhiyun.. note::
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun   The :ref:`ref-tasks-kernel_configcheck` task can also optionally report if
1479*4882a593Smuzhiyun   an option is overridden during processing.
1480*4882a593Smuzhiyun
1481*4882a593SmuzhiyunFor each output warning, a message points to the file that contains a
1482*4882a593Smuzhiyunlist of the options and a pointer to the configuration fragment that
1483*4882a593Smuzhiyundefines them. Collectively, the files are the key to streamlining the
1484*4882a593Smuzhiyunconfiguration.
1485*4882a593Smuzhiyun
1486*4882a593SmuzhiyunTo streamline the configuration, do the following:
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun1. *Use a Working Configuration:* Start with a full configuration that
1489*4882a593Smuzhiyun   you know works. Be sure the configuration builds and boots
1490*4882a593Smuzhiyun   successfully. Use this configuration file as your baseline.
1491*4882a593Smuzhiyun
1492*4882a593Smuzhiyun2. *Run Configure and Check Tasks:* Separately run the
1493*4882a593Smuzhiyun   ``do_kernel_configme`` and ``do_kernel_configcheck`` tasks::
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun      $ bitbake linux-yocto -c kernel_configme -f
1496*4882a593Smuzhiyun      $ bitbake linux-yocto -c kernel_configcheck -f
1497*4882a593Smuzhiyun
1498*4882a593Smuzhiyun3. *Process the Results:* Take the resulting list of files from the
1499*4882a593Smuzhiyun   ``do_kernel_configcheck`` task warnings and do the following:
1500*4882a593Smuzhiyun
1501*4882a593Smuzhiyun   -  Drop values that are redefined in the fragment but do not change
1502*4882a593Smuzhiyun      the final ``.config`` file.
1503*4882a593Smuzhiyun
1504*4882a593Smuzhiyun   -  Analyze and potentially drop values from the ``.config`` file that
1505*4882a593Smuzhiyun      override required configurations.
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun   -  Analyze and potentially remove non-board specific options.
1508*4882a593Smuzhiyun
1509*4882a593Smuzhiyun   -  Remove repeated and invalid options.
1510*4882a593Smuzhiyun
1511*4882a593Smuzhiyun4. *Re-Run Configure and Check Tasks:* After you have worked through the
1512*4882a593Smuzhiyun   output of the kernel configuration audit, you can re-run the
1513*4882a593Smuzhiyun   ``do_kernel_configme`` and ``do_kernel_configcheck`` tasks to see the
1514*4882a593Smuzhiyun   results of your changes. If you have more issues, you can deal with
1515*4882a593Smuzhiyun   them as described in the previous step.
1516*4882a593Smuzhiyun
1517*4882a593SmuzhiyunIteratively working through steps two through four eventually yields a
1518*4882a593Smuzhiyunminimal, streamlined configuration file. Once you have the best
1519*4882a593Smuzhiyun``.config``, you can build the Linux Yocto kernel.
1520*4882a593Smuzhiyun
1521*4882a593SmuzhiyunExpanding Variables
1522*4882a593Smuzhiyun===================
1523*4882a593Smuzhiyun
1524*4882a593SmuzhiyunSometimes it is helpful to determine what a variable expands to during a
1525*4882a593Smuzhiyunbuild. You can examine the values of variables by examining the
1526*4882a593Smuzhiyunoutput of the ``bitbake -e`` command. The output is long and is more
1527*4882a593Smuzhiyuneasily managed in a text file, which allows for easy searches::
1528*4882a593Smuzhiyun
1529*4882a593Smuzhiyun   $ bitbake -e virtual/kernel > some_text_file
1530*4882a593Smuzhiyun
1531*4882a593SmuzhiyunWithin the text file, you can see
1532*4882a593Smuzhiyunexactly how each variable is expanded and used by the OpenEmbedded build
1533*4882a593Smuzhiyunsystem.
1534*4882a593Smuzhiyun
1535*4882a593SmuzhiyunWorking with a "Dirty" Kernel Version String
1536*4882a593Smuzhiyun============================================
1537*4882a593Smuzhiyun
1538*4882a593SmuzhiyunIf you build a kernel image and the version string has a "+" or a
1539*4882a593Smuzhiyun"-dirty" at the end, it means there are uncommitted modifications in the kernel's
1540*4882a593Smuzhiyunsource directory. Follow these steps to clean up the version string:
1541*4882a593Smuzhiyun
1542*4882a593Smuzhiyun1. *Discover the Uncommitted Changes:* Go to the kernel's locally cloned
1543*4882a593Smuzhiyun   Git repository (source directory) and use the following Git command
1544*4882a593Smuzhiyun   to list the files that have been changed, added, or removed::
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun      $ git status
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun2. *Commit the Changes:* You should commit those changes to the kernel
1549*4882a593Smuzhiyun   source tree regardless of whether or not you will save, export, or
1550*4882a593Smuzhiyun   use the changes::
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun      $ git add
1553*4882a593Smuzhiyun      $ git commit -s -a -m "getting rid of -dirty"
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun3. *Rebuild the Kernel Image:* Once you commit the changes, rebuild the
1556*4882a593Smuzhiyun   kernel.
1557*4882a593Smuzhiyun
1558*4882a593Smuzhiyun   Depending on your particular kernel development workflow, the
1559*4882a593Smuzhiyun   commands you use to rebuild the kernel might differ. For information
1560*4882a593Smuzhiyun   on building the kernel image when using ``devtool``, see the
1561*4882a593Smuzhiyun   ":ref:`kernel-dev/common:using \`\`devtool\`\` to patch the kernel`"
1562*4882a593Smuzhiyun   section. For
1563*4882a593Smuzhiyun   information on building the kernel image when using Bitbake, see the
1564*4882a593Smuzhiyun   ":ref:`kernel-dev/common:using traditional kernel development to patch the kernel`"
1565*4882a593Smuzhiyun   section.
1566*4882a593Smuzhiyun
1567*4882a593SmuzhiyunWorking With Your Own Sources
1568*4882a593Smuzhiyun=============================
1569*4882a593Smuzhiyun
1570*4882a593SmuzhiyunIf you cannot work with one of the Linux kernel versions supported by
1571*4882a593Smuzhiyunexisting linux-yocto recipes, you can still make use of the Yocto
1572*4882a593SmuzhiyunProject Linux kernel tooling by working with your own sources. When you
1573*4882a593Smuzhiyunuse your own sources, you will not be able to leverage the existing
1574*4882a593Smuzhiyunkernel :term:`Metadata` and stabilization
1575*4882a593Smuzhiyunwork of the linux-yocto sources. However, you will be able to manage
1576*4882a593Smuzhiyunyour own Metadata in the same format as the linux-yocto sources.
1577*4882a593SmuzhiyunMaintaining format compatibility facilitates converging with linux-yocto
1578*4882a593Smuzhiyunon a future, mutually-supported kernel version.
1579*4882a593Smuzhiyun
1580*4882a593SmuzhiyunTo help you use your own sources, the Yocto Project provides a
1581*4882a593Smuzhiyunlinux-yocto custom recipe that uses ``kernel.org`` sources and
1582*4882a593Smuzhiyunthe Yocto Project Linux kernel tools for managing kernel Metadata.
1583*4882a593SmuzhiyunYou can find this recipe in the ``poky`` Git repository:
1584*4882a593Smuzhiyun:yocto_git:`meta-skeleton/recipes-kernel/linux/linux-yocto-custom.bb
1585*4882a593Smuzhiyun</poky/tree/meta-skeleton/recipes-kernel/linux/linux-yocto-custom.bb>`.
1586*4882a593Smuzhiyun
1587*4882a593SmuzhiyunHere are some basic steps you can use to work with your own sources:
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun1. *Create a Copy of the Kernel Recipe:* Copy the
1590*4882a593Smuzhiyun   ``linux-yocto-custom.bb`` recipe to your layer and give it a
1591*4882a593Smuzhiyun   meaningful name. The name should include the version of the Yocto
1592*4882a593Smuzhiyun   Linux kernel you are using (e.g. ``linux-yocto-myproject_4.12.bb``,
1593*4882a593Smuzhiyun   where "4.12" is the base version of the Linux kernel with which you
1594*4882a593Smuzhiyun   would be working).
1595*4882a593Smuzhiyun
1596*4882a593Smuzhiyun2. *Create a Directory for Your Patches:* In the same directory inside
1597*4882a593Smuzhiyun   your layer, create a matching directory to store your patches and
1598*4882a593Smuzhiyun   configuration files (e.g. ``linux-yocto-myproject``).
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun3. *Ensure You Have Configurations:* Make sure you have either a
1601*4882a593Smuzhiyun   ``defconfig`` file or configuration fragment files in your layer.
1602*4882a593Smuzhiyun   When you use the ``linux-yocto-custom.bb`` recipe, you must specify a
1603*4882a593Smuzhiyun   configuration. If you do not have a ``defconfig`` file, you can run
1604*4882a593Smuzhiyun   the following::
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun      $ make defconfig
1607*4882a593Smuzhiyun
1608*4882a593Smuzhiyun   After running the command, copy the
1609*4882a593Smuzhiyun   resulting ``.config`` file to the ``files`` directory in your layer
1610*4882a593Smuzhiyun   as "defconfig" and then add it to the
1611*4882a593Smuzhiyun   :term:`SRC_URI` variable in the
1612*4882a593Smuzhiyun   recipe.
1613*4882a593Smuzhiyun
1614*4882a593Smuzhiyun   Running the ``make defconfig`` command results in the default
1615*4882a593Smuzhiyun   configuration for your architecture as defined by your kernel.
1616*4882a593Smuzhiyun   However, there is no guarantee that this configuration is valid for
1617*4882a593Smuzhiyun   your use case, or that your board will even boot. This is
1618*4882a593Smuzhiyun   particularly true for non-x86 architectures.
1619*4882a593Smuzhiyun
1620*4882a593Smuzhiyun   To use non-x86 ``defconfig`` files, you need to be more specific and
1621*4882a593Smuzhiyun   find one that matches your board (i.e. for arm, you look in
1622*4882a593Smuzhiyun   ``arch/arm/configs`` and use the one that is the best starting point
1623*4882a593Smuzhiyun   for your board).
1624*4882a593Smuzhiyun
1625*4882a593Smuzhiyun4. *Edit the Recipe:* Edit the following variables in your recipe as
1626*4882a593Smuzhiyun   appropriate for your project:
1627*4882a593Smuzhiyun
1628*4882a593Smuzhiyun   -  :term:`SRC_URI`: The
1629*4882a593Smuzhiyun      :term:`SRC_URI` should specify a Git repository that uses one of the
1630*4882a593Smuzhiyun      supported Git fetcher protocols (i.e. ``file``, ``git``, ``http``,
1631*4882a593Smuzhiyun      and so forth). The :term:`SRC_URI` variable should also specify either
1632*4882a593Smuzhiyun      a ``defconfig`` file or some configuration fragment files. The
1633*4882a593Smuzhiyun      skeleton recipe provides an example :term:`SRC_URI` as a syntax
1634*4882a593Smuzhiyun      reference.
1635*4882a593Smuzhiyun
1636*4882a593Smuzhiyun   -  :term:`LINUX_VERSION`:
1637*4882a593Smuzhiyun      The Linux kernel version you are using (e.g. "4.12").
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun   -  :term:`LINUX_VERSION_EXTENSION`:
1640*4882a593Smuzhiyun      The Linux kernel ``CONFIG_LOCALVERSION`` that is compiled into the
1641*4882a593Smuzhiyun      resulting kernel and visible through the ``uname`` command.
1642*4882a593Smuzhiyun
1643*4882a593Smuzhiyun   -  :term:`SRCREV`: The commit ID
1644*4882a593Smuzhiyun      from which you want to build.
1645*4882a593Smuzhiyun
1646*4882a593Smuzhiyun   -  :term:`PR`: Treat this variable the
1647*4882a593Smuzhiyun      same as you would in any other recipe. Increment the variable to
1648*4882a593Smuzhiyun      indicate to the OpenEmbedded build system that the recipe has
1649*4882a593Smuzhiyun      changed.
1650*4882a593Smuzhiyun
1651*4882a593Smuzhiyun   -  :term:`PV`: The default :term:`PV`
1652*4882a593Smuzhiyun      assignment is typically adequate. It combines the
1653*4882a593Smuzhiyun      :term:`LINUX_VERSION` with the Source Control Manager (SCM) revision
1654*4882a593Smuzhiyun      as derived from the :term:`SRCPV`
1655*4882a593Smuzhiyun      variable. The combined results are a string with the following
1656*4882a593Smuzhiyun      form::
1657*4882a593Smuzhiyun
1658*4882a593Smuzhiyun         3.19.11+git1+68a635bf8dfb64b02263c1ac80c948647cc76d5f_1+218bd8d2022b9852c60d32f0d770931e3cf343e2
1659*4882a593Smuzhiyun
1660*4882a593Smuzhiyun      While lengthy, the extra verbosity in :term:`PV` helps ensure you are
1661*4882a593Smuzhiyun      using the exact sources from which you intend to build.
1662*4882a593Smuzhiyun
1663*4882a593Smuzhiyun   -  :term:`COMPATIBLE_MACHINE`:
1664*4882a593Smuzhiyun      A list of the machines supported by your new recipe. This variable
1665*4882a593Smuzhiyun      in the example recipe is set by default to a regular expression
1666*4882a593Smuzhiyun      that matches only the empty string, "(^$)". This default setting
1667*4882a593Smuzhiyun      triggers an explicit build failure. You must change it to match a
1668*4882a593Smuzhiyun      list of the machines that your new recipe supports. For example,
1669*4882a593Smuzhiyun      to support the ``qemux86`` and ``qemux86-64`` machines, use the
1670*4882a593Smuzhiyun      following form::
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun         COMPATIBLE_MACHINE = "qemux86|qemux86-64"
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun5. *Customize Your Recipe as Needed:* Provide further customizations to
1675*4882a593Smuzhiyun   your recipe as needed just as you would customize an existing
1676*4882a593Smuzhiyun   linux-yocto recipe. See the
1677*4882a593Smuzhiyun   ":ref:`ref-manual/devtool-reference:modifying an existing recipe`" section
1678*4882a593Smuzhiyun   for information.
1679*4882a593Smuzhiyun
1680*4882a593SmuzhiyunWorking with Out-of-Tree Modules
1681*4882a593Smuzhiyun================================
1682*4882a593Smuzhiyun
1683*4882a593SmuzhiyunThis section describes steps to build out-of-tree modules on your target
1684*4882a593Smuzhiyunand describes how to incorporate out-of-tree modules in the build.
1685*4882a593Smuzhiyun
1686*4882a593SmuzhiyunBuilding Out-of-Tree Modules on the Target
1687*4882a593Smuzhiyun------------------------------------------
1688*4882a593Smuzhiyun
1689*4882a593SmuzhiyunWhile the traditional Yocto Project development model would be to
1690*4882a593Smuzhiyuninclude kernel modules as part of the normal build process, you might
1691*4882a593Smuzhiyunfind it useful to build modules on the target. This could be the case if
1692*4882a593Smuzhiyunyour target system is capable and powerful enough to handle the
1693*4882a593Smuzhiyunnecessary compilation. Before deciding to build on your target, however,
1694*4882a593Smuzhiyunyou should consider the benefits of using a proper cross-development
1695*4882a593Smuzhiyunenvironment from your build host.
1696*4882a593Smuzhiyun
1697*4882a593SmuzhiyunIf you want to be able to build out-of-tree modules on the target, there
1698*4882a593Smuzhiyunare some steps you need to take on the target that is running your SDK
1699*4882a593Smuzhiyunimage. Briefly, the ``kernel-dev`` package is installed by default on
1700*4882a593Smuzhiyunall ``*.sdk`` images and the ``kernel-devsrc`` package is installed on
1701*4882a593Smuzhiyunmany of the ``*.sdk`` images. However, you need to create some scripts
1702*4882a593Smuzhiyunprior to attempting to build the out-of-tree modules on the target that
1703*4882a593Smuzhiyunis running that image.
1704*4882a593Smuzhiyun
1705*4882a593SmuzhiyunPrior to attempting to build the out-of-tree modules, you need to be on
1706*4882a593Smuzhiyunthe target as root and you need to change to the ``/usr/src/kernel``
1707*4882a593Smuzhiyundirectory. Next, ``make`` the scripts:
1708*4882a593Smuzhiyun
1709*4882a593Smuzhiyun.. code-block:: none
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun   # cd /usr/src/kernel
1712*4882a593Smuzhiyun   # make scripts
1713*4882a593Smuzhiyun
1714*4882a593SmuzhiyunBecause all SDK image recipes include ``dev-pkgs``, the
1715*4882a593Smuzhiyun``kernel-dev`` packages will be installed as part of the SDK image and
1716*4882a593Smuzhiyunthe ``kernel-devsrc`` packages will be installed as part of applicable
1717*4882a593SmuzhiyunSDK images. The SDK uses the scripts when building out-of-tree modules.
1718*4882a593SmuzhiyunOnce you have switched to that directory and created the scripts, you
1719*4882a593Smuzhiyunshould be able to build your out-of-tree modules on the target.
1720*4882a593Smuzhiyun
1721*4882a593SmuzhiyunIncorporating Out-of-Tree Modules
1722*4882a593Smuzhiyun---------------------------------
1723*4882a593Smuzhiyun
1724*4882a593SmuzhiyunWhile it is always preferable to work with sources integrated into the
1725*4882a593SmuzhiyunLinux kernel sources, if you need an external kernel module, the
1726*4882a593Smuzhiyun``hello-mod.bb`` recipe is available as a template from which you can
1727*4882a593Smuzhiyuncreate your own out-of-tree Linux kernel module recipe.
1728*4882a593Smuzhiyun
1729*4882a593SmuzhiyunThis template recipe is located in the ``poky`` Git repository of the
1730*4882a593SmuzhiyunYocto Project:
1731*4882a593Smuzhiyun:yocto_git:`meta-skeleton/recipes-kernel/hello-mod/hello-mod_0.1.bb
1732*4882a593Smuzhiyun</poky/tree/meta-skeleton/recipes-kernel/hello-mod/hello-mod_0.1.bb>`.
1733*4882a593Smuzhiyun
1734*4882a593SmuzhiyunTo get started, copy this recipe to your layer and give it a meaningful
1735*4882a593Smuzhiyunname (e.g. ``mymodule_1.0.bb``). In the same directory, create a new
1736*4882a593Smuzhiyundirectory named ``files`` where you can store any source files, patches,
1737*4882a593Smuzhiyunor other files necessary for building the module that do not come with
1738*4882a593Smuzhiyunthe sources. Finally, update the recipe as needed for the module.
1739*4882a593SmuzhiyunTypically, you will need to set the following variables:
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun-  :term:`DESCRIPTION`
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun-  :term:`LICENSE* <LICENSE>`
1744*4882a593Smuzhiyun
1745*4882a593Smuzhiyun-  :term:`SRC_URI`
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun-  :term:`PV`
1748*4882a593Smuzhiyun
1749*4882a593SmuzhiyunDepending on the build system used by the module sources, you might need
1750*4882a593Smuzhiyunto make some adjustments. For example, a typical module ``Makefile``
1751*4882a593Smuzhiyunlooks much like the one provided with the ``hello-mod`` template::
1752*4882a593Smuzhiyun
1753*4882a593Smuzhiyun   obj-m := hello.o
1754*4882a593Smuzhiyun
1755*4882a593Smuzhiyun   SRC := $(shell pwd)
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun   all:
1758*4882a593Smuzhiyun   	$(MAKE) -C $(KERNEL_SRC) M=$(SRC)
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun   modules_install:
1761*4882a593Smuzhiyun   	$(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install
1762*4882a593Smuzhiyun   ...
1763*4882a593Smuzhiyun
1764*4882a593SmuzhiyunThe important point to note here is the :term:`KERNEL_SRC` variable. The
1765*4882a593Smuzhiyun:ref:`module <ref-classes-module>` class sets this variable and the
1766*4882a593Smuzhiyun:term:`KERNEL_PATH` variable to
1767*4882a593Smuzhiyun``${STAGING_KERNEL_DIR}`` with the necessary Linux kernel build
1768*4882a593Smuzhiyuninformation to build modules. If your module ``Makefile`` uses a
1769*4882a593Smuzhiyundifferent variable, you might want to override the
1770*4882a593Smuzhiyun:ref:`ref-tasks-compile` step, or
1771*4882a593Smuzhiyuncreate a patch to the ``Makefile`` to work with the more typical
1772*4882a593Smuzhiyun:term:`KERNEL_SRC` or :term:`KERNEL_PATH` variables.
1773*4882a593Smuzhiyun
1774*4882a593SmuzhiyunAfter you have prepared your recipe, you will likely want to include the
1775*4882a593Smuzhiyunmodule in your images. To do this, see the documentation for the
1776*4882a593Smuzhiyunfollowing variables in the Yocto Project Reference Manual and set one of
1777*4882a593Smuzhiyunthem appropriately for your machine configuration file:
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun-  :term:`MACHINE_ESSENTIAL_EXTRA_RDEPENDS`
1780*4882a593Smuzhiyun
1781*4882a593Smuzhiyun-  :term:`MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS`
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun-  :term:`MACHINE_EXTRA_RDEPENDS`
1784*4882a593Smuzhiyun
1785*4882a593Smuzhiyun-  :term:`MACHINE_EXTRA_RRECOMMENDS`
1786*4882a593Smuzhiyun
1787*4882a593SmuzhiyunModules are often not required for boot and can be excluded from certain
1788*4882a593Smuzhiyunbuild configurations. The following allows for the most flexibility::
1789*4882a593Smuzhiyun
1790*4882a593Smuzhiyun   MACHINE_EXTRA_RRECOMMENDS += "kernel-module-mymodule"
1791*4882a593Smuzhiyun
1792*4882a593SmuzhiyunThe value is
1793*4882a593Smuzhiyunderived by appending the module filename without the ``.ko`` extension
1794*4882a593Smuzhiyunto the string "kernel-module-".
1795*4882a593Smuzhiyun
1796*4882a593SmuzhiyunBecause the variable is
1797*4882a593Smuzhiyun:term:`RRECOMMENDS` and not a
1798*4882a593Smuzhiyun:term:`RDEPENDS` variable, the build
1799*4882a593Smuzhiyunwill not fail if this module is not available to include in the image.
1800*4882a593Smuzhiyun
1801*4882a593SmuzhiyunInspecting Changes and Commits
1802*4882a593Smuzhiyun==============================
1803*4882a593Smuzhiyun
1804*4882a593SmuzhiyunA common question when working with a kernel is: "What changes have been
1805*4882a593Smuzhiyunapplied to this tree?" Rather than using "grep" across directories to
1806*4882a593Smuzhiyunsee what has changed, you can use Git to inspect or search the kernel
1807*4882a593Smuzhiyuntree. Using Git is an efficient way to see what has changed in the tree.
1808*4882a593Smuzhiyun
1809*4882a593SmuzhiyunWhat Changed in a Kernel?
1810*4882a593Smuzhiyun-------------------------
1811*4882a593Smuzhiyun
1812*4882a593SmuzhiyunFollowing are a few examples that show how to use Git commands to
1813*4882a593Smuzhiyunexamine changes. These examples are by no means the only way to see
1814*4882a593Smuzhiyunchanges.
1815*4882a593Smuzhiyun
1816*4882a593Smuzhiyun.. note::
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun   In the following examples, unless you provide a commit range, ``kernel.org``
1819*4882a593Smuzhiyun   history is blended with Yocto Project kernel changes. You can form
1820*4882a593Smuzhiyun   ranges by using branch names from the kernel tree as the upper and
1821*4882a593Smuzhiyun   lower commit markers with the Git commands. You can see the branch
1822*4882a593Smuzhiyun   names through the web interface to the Yocto Project source
1823*4882a593Smuzhiyun   repositories at :yocto_git:`/`.
1824*4882a593Smuzhiyun
1825*4882a593SmuzhiyunTo see a full range of the changes, use the ``git whatchanged`` command
1826*4882a593Smuzhiyunand specify a commit range for the branch (`commit`\ ``..``\ `commit`).
1827*4882a593Smuzhiyun
1828*4882a593SmuzhiyunHere is an example that looks at what has changed in the ``emenlow``
1829*4882a593Smuzhiyunbranch of the ``linux-yocto-3.19`` kernel. The lower commit range is the
1830*4882a593Smuzhiyuncommit associated with the ``standard/base`` branch, while the upper
1831*4882a593Smuzhiyuncommit range is the commit associated with the ``standard/emenlow``
1832*4882a593Smuzhiyunbranch.
1833*4882a593Smuzhiyun::
1834*4882a593Smuzhiyun
1835*4882a593Smuzhiyun   $ git whatchanged origin/standard/base..origin/standard/emenlow
1836*4882a593Smuzhiyun
1837*4882a593SmuzhiyunTo see short, one line summaries of changes use the ``git log`` command::
1838*4882a593Smuzhiyun
1839*4882a593Smuzhiyun   $ git log --oneline origin/standard/base..origin/standard/emenlow
1840*4882a593Smuzhiyun
1841*4882a593SmuzhiyunUse this command to see code differences for the changes::
1842*4882a593Smuzhiyun
1843*4882a593Smuzhiyun   $ git diff origin/standard/base..origin/standard/emenlow
1844*4882a593Smuzhiyun
1845*4882a593SmuzhiyunUse this command to see the commit log messages and the text
1846*4882a593Smuzhiyundifferences::
1847*4882a593Smuzhiyun
1848*4882a593Smuzhiyun   $ git show origin/standard/base..origin/standard/emenlow
1849*4882a593Smuzhiyun
1850*4882a593SmuzhiyunUse this command to create individual patches for each change. Here is
1851*4882a593Smuzhiyunan example that creates patch files for each commit and places them
1852*4882a593Smuzhiyunin your ``Documents`` directory::
1853*4882a593Smuzhiyun
1854*4882a593Smuzhiyun   $ git format-patch -o $HOME/Documents origin/standard/base..origin/standard/emenlow
1855*4882a593Smuzhiyun
1856*4882a593SmuzhiyunShowing a Particular Feature or Branch Change
1857*4882a593Smuzhiyun---------------------------------------------
1858*4882a593Smuzhiyun
1859*4882a593SmuzhiyunTags in the Yocto Project kernel tree divide changes for significant
1860*4882a593Smuzhiyunfeatures or branches. The ``git show`` tag command shows changes based
1861*4882a593Smuzhiyunon a tag. Here is an example that shows ``systemtap`` changes::
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun   $ git show systemtap
1864*4882a593Smuzhiyun
1865*4882a593SmuzhiyunYou can use the ``git branch --contains`` tag command to
1866*4882a593Smuzhiyunshow the branches that contain a particular feature. This command shows
1867*4882a593Smuzhiyunthe branches that contain the ``systemtap`` feature::
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun   $ git branch --contains systemtap
1870*4882a593Smuzhiyun
1871*4882a593SmuzhiyunAdding Recipe-Space Kernel Features
1872*4882a593Smuzhiyun===================================
1873*4882a593Smuzhiyun
1874*4882a593SmuzhiyunYou can add kernel features in the
1875*4882a593Smuzhiyun:ref:`recipe-space <kernel-dev/advanced:recipe-space metadata>`
1876*4882a593Smuzhiyunby using the :term:`KERNEL_FEATURES`
1877*4882a593Smuzhiyunvariable and by specifying the feature's ``.scc`` file path in the
1878*4882a593Smuzhiyun:term:`SRC_URI` statement. When you
1879*4882a593Smuzhiyunadd features using this method, the OpenEmbedded build system checks to
1880*4882a593Smuzhiyunbe sure the features are present. If the features are not present, the
1881*4882a593Smuzhiyunbuild stops. Kernel features are the last elements processed for
1882*4882a593Smuzhiyunconfiguring and patching the kernel. Therefore, adding features in this
1883*4882a593Smuzhiyunmanner is a way to enforce specific features are present and enabled
1884*4882a593Smuzhiyunwithout needing to do a full audit of any other layer's additions to the
1885*4882a593Smuzhiyun:term:`SRC_URI` statement.
1886*4882a593Smuzhiyun
1887*4882a593SmuzhiyunYou add a kernel feature by providing the feature as part of the
1888*4882a593Smuzhiyun:term:`KERNEL_FEATURES` variable and by providing the path to the feature's
1889*4882a593Smuzhiyun``.scc`` file, which is relative to the root of the kernel Metadata. The
1890*4882a593SmuzhiyunOpenEmbedded build system searches all forms of kernel Metadata on the
1891*4882a593Smuzhiyun:term:`SRC_URI` statement regardless of whether the Metadata is in the
1892*4882a593Smuzhiyun"kernel-cache", system kernel Metadata, or a recipe-space Metadata (i.e.
1893*4882a593Smuzhiyunpart of the kernel recipe). See the
1894*4882a593Smuzhiyun":ref:`kernel-dev/advanced:kernel metadata location`" section for
1895*4882a593Smuzhiyunadditional information.
1896*4882a593Smuzhiyun
1897*4882a593SmuzhiyunWhen you specify the feature's ``.scc`` file on the :term:`SRC_URI`
1898*4882a593Smuzhiyunstatement, the OpenEmbedded build system adds the directory of that
1899*4882a593Smuzhiyun``.scc`` file along with all its subdirectories to the kernel feature
1900*4882a593Smuzhiyunsearch path. Because subdirectories are searched, you can reference a
1901*4882a593Smuzhiyunsingle ``.scc`` file in the :term:`SRC_URI` statement to reference multiple
1902*4882a593Smuzhiyunkernel features.
1903*4882a593Smuzhiyun
1904*4882a593SmuzhiyunConsider the following example that adds the "test.scc" feature to the
1905*4882a593Smuzhiyunbuild.
1906*4882a593Smuzhiyun
1907*4882a593Smuzhiyun1. *Create the Feature File:* Create a ``.scc`` file and locate it just
1908*4882a593Smuzhiyun   as you would any other patch file, ``.cfg`` file, or fetcher item you
1909*4882a593Smuzhiyun   specify in the :term:`SRC_URI` statement.
1910*4882a593Smuzhiyun
1911*4882a593Smuzhiyun   .. note::
1912*4882a593Smuzhiyun
1913*4882a593Smuzhiyun      -  You must add the directory of the ``.scc`` file to the
1914*4882a593Smuzhiyun         fetcher's search path in the same manner as you would add a
1915*4882a593Smuzhiyun         ``.patch`` file.
1916*4882a593Smuzhiyun
1917*4882a593Smuzhiyun      -  You can create additional ``.scc`` files beneath the directory
1918*4882a593Smuzhiyun         that contains the file you are adding. All subdirectories are
1919*4882a593Smuzhiyun         searched during the build as potential feature directories.
1920*4882a593Smuzhiyun
1921*4882a593Smuzhiyun   Continuing with the example, suppose the "test.scc" feature you are
1922*4882a593Smuzhiyun   adding has a ``test.scc`` file in the following directory::
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun      my_recipe
1925*4882a593Smuzhiyun      |
1926*4882a593Smuzhiyun      +-linux-yocto
1927*4882a593Smuzhiyun         |
1928*4882a593Smuzhiyun         +-test.cfg
1929*4882a593Smuzhiyun         +-test.scc
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun   In this example, the
1932*4882a593Smuzhiyun   ``linux-yocto`` directory has both the feature ``test.scc`` file and
1933*4882a593Smuzhiyun   a similarly named configuration fragment file ``test.cfg``.
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun2. *Add the Feature File to SRC_URI:* Add the ``.scc`` file to the
1936*4882a593Smuzhiyun   recipe's :term:`SRC_URI` statement::
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun      SRC_URI:append = " file://test.scc"
1939*4882a593Smuzhiyun
1940*4882a593Smuzhiyun   The leading space before the path is important as the path is
1941*4882a593Smuzhiyun   appended to the existing path.
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun3. *Specify the Feature as a Kernel Feature:* Use the
1944*4882a593Smuzhiyun   :term:`KERNEL_FEATURES` statement to specify the feature as a kernel
1945*4882a593Smuzhiyun   feature::
1946*4882a593Smuzhiyun
1947*4882a593Smuzhiyun      KERNEL_FEATURES:append = " test.scc"
1948*4882a593Smuzhiyun
1949*4882a593Smuzhiyun   The OpenEmbedded build
1950*4882a593Smuzhiyun   system processes the kernel feature when it builds the kernel.
1951*4882a593Smuzhiyun
1952*4882a593Smuzhiyun   .. note::
1953*4882a593Smuzhiyun
1954*4882a593Smuzhiyun      If other features are contained below "test.scc", then their
1955*4882a593Smuzhiyun      directories are relative to the directory containing the ``test.scc``
1956*4882a593Smuzhiyun      file.
1957